- 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
Listimplementations are expected to provide fast element access by index, they implement the
- Elements stored in these collections/maps must support typical collections/maps contracts, including proper support for
equals(). If an element of a
Setor a key of a
Mapis mutated in a way that affects its
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.
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