Java 9 brings a lot of big new features, tools and API enhancements. But somehow the most excitement comes from a few simple additions to collections at which we will look today.
JEP 269: Convenience Factory Methods for Collections
So why so many people are so excited about JEP 269? Creating a collection from a number of elements is actually an operation very frequently used in the code. How many times did you wrote something like:Any of these options require more than one operation and creation of additional objects just to get an immutable collection. Also Arrays#asList method feels slightly out of place. That's why Java 9 introduce these static factory methods for creating immutable collections, to make the process much more easier. A number of methods is provided to cover different number of elements: There's also the same kind of methods present for List as well:
Such variety of methods is based on performance reasons. Having just one var-arg method would mean that the unnecessary array will be created all the time. Here's how the code from examples shown above would look like using Java 9:
Let's have a look at some properties of Collections created with these factory methods.
Properties of Set created with static factory methods
Here's a few interesting properties for such Sets. They
- are structurally immutable, which means that no element can be added or removed from it.
- disallow null as values. If null is passed a NullPointerException will be thrown.
- disallow duplicates. If duplicate elements are passed to such factory method - an IlligalArgumentException will be thrown.
- do not guarantee the iteration order through elements.
Properties of List created with static factory methods
Properties of lists are very similar to the ones that Set has. Lists created with factory methods:
- are structurally immutable, which means that no element can be added or removed from it.
- disallow null as values. If null is passed a NullPointerException will be thrown.
- guarantee the iteration order through elements will be the same as they were passed to a factory method.
And the examples showing these properties:
Properties of Map created with static factory methods
Map also did receive an update in form of static factory methods for immutable map creation. There are two kinds of methods present for it Map#of and Map#ofEntries :
First variation of methods - Map#of - receives a sequence of key/value pairs, while the second variation receives a sequence of Map.Entry elements. For ease of use of Map#ofEntries there's also a Map#entry which creates an entry.
Keep in mind that even though Map#ofEntries is much more readable it uses a var-arg approach.
Now as for the properties of such immutable maps. Such Maps:
Keep in mind that even though Map#ofEntries is much more readable it uses a var-arg approach.
Now as for the properties of such immutable maps. Such Maps:
- are structurally immutable, which means that no element can be added, removed or updated in it. Keep in mind though that if the object in the map itself is mutable you would be able to change it's state. This may cause inconsistent behavior of the map.
- disallow null as either keys or values. If null is passed a NullPointerException will be thrown.
- disallow duplicates. If duplicate elements are passed to such factory method - an IlligalArgumentException will be thrown.
- do not guarantee the iteration order through elements.
And here are a few examples that show these properties:
Having these static factory methods on collections is definitely a nice addition to the language. They allow to write nicer and cleaner code without adding additional dependencies which could provide such capabilities (like for example Guava) or creating your own set of utility methods to cover these cases.
To be continued...
Useful post. Thanks!
ReplyDelete