Class UnmodifiableCollectorsExamples
java.lang.Object
net.jrodolfo.java_evolution.java10.UnmodifiableCollectorsExamples
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSmall Java 10-style data class used by the collector examples. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCollects active feature names into an unmodifiable list.Collects features into an unmodifiable map keyed by feature name.Attempts to collect duplicate keys into an unmodifiable map.Collects feature categories into an unmodifiable set.
-
Constructor Details
-
UnmodifiableCollectorsExamples
public UnmodifiableCollectorsExamples()
-
-
Method Details
-
activeFeatureNames
Collects active feature names into an unmodifiable list.- Parameters:
features- the features to inspect- Returns:
- active feature names in encounter order
-
featureCategories
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
-