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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.