Class UnmodifiableCollectorsExamples

java.lang.Object
net.jrodolfo.java_evolution.java10.UnmodifiableCollectorsExamples

public class UnmodifiableCollectorsExamples extends Object
Demonstrates unmodifiable collectors introduced in Java 10.

Java 8 streams made it easy to collect pipeline results, but the standard collectors returned mutable collections. Code that wanted to return a read-only result needed an extra wrapping or copying step.

Java 10 added Collectors.toUnmodifiableList(), Collectors.toUnmodifiableSet(), and Collectors.toUnmodifiableMap(Function, Function). These collectors produce collections that reject mutation after the stream pipeline completes.

  • Constructor Details

    • UnmodifiableCollectorsExamples

      public UnmodifiableCollectorsExamples()
  • Method Details

    • activeFeatureNames

      public List<String> activeFeatureNames(List<UnmodifiableCollectorsExamples.Feature> features)
      Collects active feature names into an unmodifiable list.
      Parameters:
      features - the features to inspect
      Returns:
      active feature names in encounter order
    • featureCategories

      public Set<String> featureCategories(List<UnmodifiableCollectorsExamples.Feature> features)
      Collects feature categories into an unmodifiable set.
      Parameters:
      features - the features to inspect
      Returns:
      unique categories from the feature list
    • categoriesByFeatureName

      public Map<String,String> categoriesByFeatureName(List<UnmodifiableCollectorsExamples.Feature> features)
      Collects features into an unmodifiable map keyed by feature name.
      Parameters:
      features - the features to index
      Returns:
      an unmodifiable map from feature name to category
    • categoriesByFeatureNameWithDuplicateKey

      public Map<String,String> categoriesByFeatureNameWithDuplicateKey(List<UnmodifiableCollectorsExamples.Feature> features)
      Attempts to collect duplicate keys into an unmodifiable map.
      Parameters:
      features - the features to index
      Returns:
      never returns when duplicate feature names exist