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
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.