mirror of
https://github.com/google/nomulus
synced 2026-08-15 11:46:08 +00:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user