question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImmutableEnum{Set,Map} Collectors

See original GitHub issue

Add 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
lowassercommented, Dec 9, 2016

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.

0reactions
Stephan202commented, Feb 26, 2017

D’oh I was looking in the wrong place. You’re right! Nice.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found