Last month’s release of Java 8 came with a slew of changes that are beginning to be picked up by the local informatics community. At this month’s San Diego Java User Group, John Clingan (Oracle Product Manager for Java EE and Glassfish) gave us an update on some of the new features. You can find out more about Java 8 at the Launch Webcast here.
Here’s a brief list of some of the new features along with some links to tutorials to help you make the most of them.
While delving into some of these features I came across some interesting surprises and side benefits associated with Java 8.
Improvements to Collections
With the addition of lambda expressions to Java, one of the long-standing holes in the language has now been filled. Most Java developers have been adding Apache Commons Collections, or Google’s Guava library to their projects to add support for Predicates, and Functions. Both of these libraries contain support for Predicate-based filtering, and Function’s that can be applied to each element in a collection.
With the latest release of Java, the new java.util.function package contains new interfaces for Predicate and Function with default methods. A removeIf method was added to the Collection interface to make it easier to filter a collection using a Predicate.
Over the years, this long-requested change to the Collections API has gone unfulfilled due to impact that such a change would have on collections like ArrayList, HashMap, etc. However, the new default method functionality made it possible to add support for functions and predicates without causing a lot of code to break.
The poorly named “Streams” API came with an extra added benefit. You can now switch from stream() to parallelStream() and execute each stream in parallel. ParallelStream is backed by a ForkJoinPool, which means that your streams automatically execute across all available processors without all the extra boilerplate code traditionally required to create and submit ForkJoinTasks.
Annotations On Method Parameters
We’re used to seeing annotations at the class level (@Entity), and at the method declaration level (@Overload), but now we can annotate method parameters, and class attributes. So if you want to indicate that a list can’t be empty you simply declare it with the @NonEmpty annotation. If you want to indicate that a String can’t be null, you can use the @NonNull. If the String is an email address use @Email. You can find some examples here and here.
For the past 6 years, Oracle has been working on JavaFX, its official replacement for Swing. JavaFX has a number of advantages over Swing (including better performance, CSS for styling, FXML for user-interface creation, and much, much more — too much to talk about here).
With Java 8, Oracle added video, and audio recording, a new default skin called Modena, printing support, a DatePicker and TreeTable, support for Nashorn in WebView, 3D support, embedded Swing components, improved styled text support via the TextFlow class, support for web fonts, numerous bug fixes and performance improvements. In addition, a new version of SceneBuilder has support for all of the latest components.
Nashorn shell scripting
Nashorn makes it even easier to create shell scripts that will run wherever you have Java installed. You simply use #!/usr/bin/jjs as your shebang line in your shell script, and off you go. You can find more information about it here. The only thing missing from all of this is support for dependency resolution. In Groovy, we have the @Grab annotation which we can use to insure that all of the dependencies needed by the script are downloaded and made available at runtime. Nashorn doesn’t have this capability yet, and we hope to see it soon.
Nashorn on the Server
Oracle’s approach to this is to make Nashorn the engine that runs that stack. The Avatar.js [Avatar Tutorial]project makes MongoDB, ExpressJS and a number of other frameworks available as part of your server ecosystem.
Project Sumatra: Java on the GPU
GPUs have gotten a lot of attention over the past few years as a means of executing computationally intensive tasks. For most developers interested in GPUs though, it has meant having to learn or re-learn the C toolset and having to port code over to run on GPUs.
Project Sumatra’s goal though is to provide the Java developer a means of letting Java code run on GPUs. This makes it possible to use the new streams capabilities across multiple GPUs.
What does this mean for informatics?
- Simpler, easier to maintain code with lambdas.
- Parallelization is now easier.
- Default methods make it easier to update older interface definitions without impacting existing code.
- Nashorn support for shell scripting makes it easier to call Java code from within shell scripts.
- Improved performance
- Ability to leverage GPUs