Java 9's other new enhancements, Part 1: Collections factory methods

Discover the new convenience factory methods that have been added to Java 9's Collections Framework

1 2 Page 2
Page 2 of 2
  • The concrete classes returned by these factories are not exposed as public APIs. No guarantees are made about the runtime type or identity of the returned collection/map, which allows the implementations to change over time without breaking compatibility. The only thing a caller can rely on is that the reference returned is an implementation of its interface type.
  • The resulting objects are serializable. A serialization proxy object is used as the common serialized form for the implementation classes. This proxy prevents information about the concrete implementations from leaking into the serialized form, which preserves flexibility for future maintenance and allows the concrete implementations to change from release to release without affecting serialization compatibility.
  • Null elements, keys, and values are disallowed. (No recently introduced collections/maps have supported nulls.) In addition, prohibiting nulls offers opportunities for a more compact internal representation, faster access, and fewer special cases.
  • Because the List implementations are expected to provide fast element access by index, they implement the java.util.RandomAccess marker interface.
  • Elements stored in these collections/maps must support typical collections/maps contracts, including proper support for hashCode() and equals(). If an element of a Set or a key of a Map is mutated in a way that affects its hashCode() or equals() methods, the behavior of the collection/map could become unspecified.
  • Once constructed and safely published, these collection/map instances will be safe for concurrent access in multiple thread contexts.


Java 9 is coming soon, and developers will need to understand its many enhancements. This series of posts attempts to impart some of that knowledge through its exploration of various enhancements apart from the widely-anticipated module system and REPL tool, which are extensively covered elsewhere.

JEP 269's small number of EnumSet-inspired factory methods minimize the pain of Java 9 not supporting collection literals. As well as reducing syntactic verbosity, these factory methods prevent any possibility of collection mutability, and so are amenable for use in Streams API-based or other parallelization contexts.

Get the source code for this post's applications. Created by Jeff Friesen for JavaWorld

The following software was used to develop the post's code:

  • 64-bit JDK 9ea+154

The post's code was tested on the following platform(s):

  • JVM on 64-bit Windows 8.1

This story, "Java 9's other new enhancements, Part 1: Collections factory methods" was originally published by JavaWorld.

Copyright © 2017 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2