Java Magazine, July/August 2017
ORACLE COM JAVAMAGAZINE JULY AUGUST 2017 22 java 9 def values Hello World from Java as Set One solution to implementing this is to provide some methods that construct collections from values and then use a varargs constructor to make them syntactically shorter similar to collection literals This is the approach used in Java 9 so you can do the following which is a lot more concise List String list List of Java 9 is here Set String set Set of Hello World from Java Maps now also have similar factory methods They work differently because Maps have keys and values rather than a single type of element For up to 10 entries Maps have overloaded constructors that take pairs of keys and values For example you could build a map of people and their ages like this Map String Integer nameToAge Map of Richard 49 Raoul 47 The varargs case for Maps is a little bit harder you need to have both keys and values but in Java methods cannot have two varargs parameters Therefore the general case is handled by taking a varargs method of Map Entry K V objects and adding a static entry method that constructs them Map String Integer nameToAge Map ofEntries entry Richard 49 entry Raoul 47 The goal here isnt just to reduce verbosity it is also to reduce the possibility of programmer errors All the collections added in recent years have banned the use of nulls as elements within collections and these collections follow suit This helps reduce the scope for bugs related to referring to null values in collections It also simplifies the internal implementation A bigger diference compared to most collections in the JDK is that these collections are immutable Immutability reduces the scope for bugs by removing the ability for one part of an application to cause problems by modifying state that another component is relying on Immutability is a concept that has been advocated by functional programming for a long time Speaking of functional programming lets look at the updates to the Streams API in Java 9 Streams Streams were a great addition to Java 8 The code that developers write using streams tends to read a lot more like the problem they are trying to solve and less code is usually required Java 9 brings some small improvements to streams ofNullable The Stream interface has a pair of factory methods called of that allow you to create streams from prespecified values one is an overload for a single value and the other takes a varargs parameter These are very useful when youre trying to test streams code and when you want to just instantiate a stream with a few values Java 9 adds an ofNullable factory Lets see how you might use this functionality Lets suppose youre trying to find a location where you can put some configuration files in a Java application You want to use a couple of diferent properties lets say app config and app home Lets write this code in Java 8 String configurationDirectory Stream of app config app home user home flatMap key final String property System getProperty key if property null return Stream empty
You must have JavaScript enabled to view digital editions.