Java gets going with version 8
Linked
As line 19 shows, you can easily perform complex tasks with the new notation, because single static methods can be linked. In the example, the comparingDouble()
method first sorts the rectangles in ascending order by width. The default method reversed()
(default methods are called Defender methods in Java 8) then reverses the order and uses thenComparing()
to return the result to the next comparator. Again, this is a Lambda expression; it sorts the rectangles in ascending order on the basis of height.
So, what appears in this case to be merely a convenience actually allows for the elegant definition of extensible interfaces. All classes that implement the Comparator interface first use the default implementation of the method. If you require different behavior, you can overload it in the class. To a great extent, this makes it possible to define object behavior at the interface level, which again leads to more compact source code compared with the previously required triad of interface, abstract class, and class in comparable tasks with previous versions.
Empowered
The MapReduce schema (used with Hadoop and other big data tools) has established itself as a standard pattern for data conversion and extraction. Along with streams, it has for the first time found its way into the Java standard library. A stream is always generated from many objects (e.g., from a list). Intermediate operations, such as converting or filtering, are then applied to the objects of the stream, and the whole thing ends with a final operation that collects the results or reduces them to a final value.
An initial example is shown in line 3 of Listing 3; this adds the areas of all rectangles of a minimum size. To do this, the code converts the list of rectangles into a stream with the new stream()
method and then applies filter()
, map()
, and sum()
methods successively to filter the results. Of course, you could do this in a loop and various intermediate steps, but with Lambdas and the predefined methods, the effort of writing this procedure is much reduced.
Listing 3
Streams in Java 8
A big advantage of streams over a simple loop is hidden in lines 7-9 of the second example. Normally, Java processes streams sequentially, but the parallel()
method requires a stream that works in parallel. In this case, Java automatically distributes the operations across multiple threads and executes them in parallel. On a modern multicore processor, this approach is much faster than sequential processing. Compared with the previously required implementation using a ThreadPool
and Future
s, the coding overhead is also much reduced.
In the example, the map()
method only converts the rectangles in the input list into objects of the Rectangle2D.Double
class; the collect()
method summarizes them in a set. For collecting, the code uses the new Collector
class. It does not just support trivial targets such as sets or lists, but also more complex things like grouped objects in maps.
Tempus Fugit
Hardly any area of the Java standard library has been as neglected as time processing. The class java.util.Date
provides only a tenuous envelope around the milliseconds elapsed since the era and does not support time zones. Also, the java.util.Calendar
class introduced in Java 1.1 totally fails to arouse the developer's enthusiasm, which is why more and more developers are now using the Joda-Time library [5].
Joda-Time, however, has a few design quirks, which is why developers led by Joda-Time author Steven Colebourne defined a completely new API for time processing (JSR 310). In Java 8, it has now become part of the normal run-time library, adding the ability to implement complex time calculations and formatting without loading an additional library. The interface has also become more secure; times are now immutable, and constants are used for days and months. As an example, Listing 4 provides a time stamp for the eve of the transition to Central European Summer Time (Figure 1), and then the correct time 25 hours later (March 30 2014 22:10:05 CEST).
Listing 4
New API for Time Processing
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.