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)