Class CollectionFactoryExamples

java.lang.Object
net.jrodolfo.java_evolution.java09.CollectionFactoryExamples

public class CollectionFactoryExamples extends Object
Demonstrates collection factory methods introduced in Java 9.

Before Java 9, creating a small read-only collection usually required several steps: create a mutable collection, populate it, and wrap it with an unmodifiable view. That made simple constants and test data noisier than the values being represented.

List.of(Object[]), Set.of(Object[]), and Map.of(Object, Object) solve that problem for small collections. They are compact, reject null, reject duplicate set elements or map keys, and return collections that cannot be mutated. The JDK can also use specialized internal implementations for small immutable collections, which is one reason these factories are preferable to hand-built wrappers for constants and examples.

  • Constructor Details

    • CollectionFactoryExamples

      public CollectionFactoryExamples()
  • Method Details