ImmutableEnum{Set,Map} Collectors
See original GitHub issueAdd collectors for enum
-specialized ImmutableSet
and ImmutableMap
:
toImmutableEnumSet()
toImmutableEnumMap(keyFunction, valueFunction)
toImmutableEnumMap(keyFunction, valueFunction, mergeFunction)
Providing these collectors in Guava would be a matter of convenience. Users could implement functionally-equivalent collectors themselves, but I think most users would not do so. I think enum
-specialized collections are underused and that it is worthwhile to promote their usage and remove friction / barriers to entry.
For example, if a user has code like this:
ImmutableSet<Month> months = stream.collect(ImmutableSet.toImmutableSet());
… then it would be cool if they could optimize their code like this:
ImmutableSet<Month> months = stream.collect(ImmutableSet.toImmutableEnumSet());
… and it would be lame if they had to do this (it would be so lame that they wouldn’t bother):
ImmutableSet<Month> months = stream.collect(SomeHelper.toImmutableEnumSet(Month.class));
// Elsewhere... javadoc and tests omitted...
public static <E extends Enum<E>> Collector<E, ?, ImmutableSet<E>>
toImmutableEnumSet(Class<E> elementType) {
Objects.requireNonNull(elementType);
return Collectors.collectingAndThen(
Collectors.toCollection(() -> EnumSet.noneOf(elementType)),
Sets::immutableEnumSet);
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top Results From Across the Web
EnumSet and EnumMap - TechEmpower Blog
EnumSet and EnumMap are compact, efficient implementations of the Set and Map interfaces. They have the constraint that their elements/keys ...
Read more >Sets (Guava: Google Core Libraries for Java 11.0 API)
Creates an EnumSet consisting of all enum values that are not in the specified collection. If the collection is an EnumSet , this...
Read more >Collect a Java Stream to an Immutable Collection - Baeldung
Learn how to collect Java Streams to immutable Collections.
Read more >Java 8 collector for Guava immutable collections?
My answer supports both types, but it does not use a Builder so it might be less efficient for immutable collections.
Read more >Sets (Guava: Google Core Libraries for Java 22.0 API)
Returns a Collector that accumulates the input elements into a new ImmutableSet with an implementation specialized for enums. Unlike ImmutableSet.toImmutableSet ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve put this into Guava’s API review queue. I’ve put together a different implementation, though, that avoids quite so much redundant copying. I’ll keep this thread updated on the status of the API review.
D’oh I was looking in the wrong place. You’re right! Nice.