That Java Guy

  • Home
  • Contact
  • About

Java 9: What's interesting and new. Collection API

by That Java Guy October 23, 2017 Collections Java

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.
Here are a few test example covering the properties mentioned above:

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:
  • 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...

READ MORE
SHARE :

About Me




Marko Bekhta


Software engineer.


Follow Me

Labels

1z0-803 1z0-804 1z0-810 Bean Validation Certification Code Europe code generation Collections Collectors conference Enum exam Hibernate Hibernate Validator Immutables Java JPA MapStruct OCA OCP Oracle Stream validation
  • Popular
  • Recent
  • Comments
    JPA: Mapping Enums the right way
    Bean Validation: Validation based on multiple properties
    JPA: Ways to map many-to-many relationships with additional information

Instagram

About

Popular Posts

  • JPA: Mapping Enums the right way
    There are quite a few different approaches one can take to map enums in entities. I would separate them into three groups: The ones which ...
  • Bean Validation: Validation based on multiple properties
    Quite often there is a need to apply validation to some of the bean's properties based on the state of the other properties of the s...
  • JPA: Ways to map many-to-many relationships with additional information
    In this post we will take a look at couple of ways how many-to-many relationships, which have additional information, can be mapped to entit...
  • JPA: a handy utility for Enums to combine with AttributeConverter
    As it was discussed in one of the previous posts JPA: Mapping Enums the right way , JPA 2.1 standardized  AttributeConverter s, which can, f...
  • Java 9: What's interesting and new. Collection API
    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...

Labels

  • Certification
  • Code Europe
  • Java
  • Oracle
  • conference
  • exam

Template Created By : ThemeXpose . All Rights Reserved.