Make ImmutableMap Stream collect()ion nicer (#654)

This adds an entriesToImmutableMap() collector that can be used in place of
toImmutableMap(Map.Entry::getkey, Map.Entry::getValue()).

It also fixes up some existing calls that use toImmutableMap() when terser
alternatives exist.
This commit is contained in:
Ben McIlwain 2020-06-26 11:57:26 -04:00 committed by GitHub
parent 660b2af990
commit d0149d75c9
15 changed files with 49 additions and 54 deletions

View file

@ -33,6 +33,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.stream.Collector;
import javax.annotation.Nullable;
/** Utility methods related to collections. */
@ -148,4 +149,11 @@ public class CollectionUtils {
}
return shards.build();
}
/**
* Returns a {@link Collector} that accumulates {@link Map.Entry}s into an {@code ImmutableMap}.
*/
public static <K, V> Collector<Map.Entry<K, V>, ?, ImmutableMap<K, V>> entriesToImmutableMap() {
return ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue);
}
}