aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-03-25 16:43:24 +0000
committerMichael Williamson <mike@zwobble.org>2016-03-25 16:43:24 +0000
commit0deccc08a51e68eb97ad6ef5a95bfd868a939169 (patch)
treef9c80cdf1f450a1d9937e65b2d07c554b748c4f2 /src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java
parent5b2235620d33776d0a1dcae8f9e68693d9390be3 (diff)
Remove use of Google collections in Notes
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java')
-rw-r--r--src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java b/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java
index 19fbfc4..873f90c 100644
--- a/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java
+++ b/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java
@@ -2,9 +2,7 @@ package org.zwobble.mammoth.internal.util;
import com.google.common.collect.ImmutableMap;
-import java.util.AbstractMap;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
import java.util.function.Function;
public class MammothMaps {
@@ -32,6 +30,10 @@ public class MammothMaps {
return Optional.ofNullable(map.get(key));
}
+ public static <T, K> Map<K, T> toMapWithKey(Iterable<T> iterable, Function<T, K> function) {
+ return toMap(iterable, element -> entry(function.apply(element), element));
+ }
+
public static <T, K, V> Map<K, V> toMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
for (T element : iterable) {
@@ -39,4 +41,26 @@ public class MammothMaps {
}
return builder.build();
}
+
+ public static <T, K> Map<K, List<T>> toMultiMapWithKey(Iterable<T> iterable, Function<T, K> function) {
+ return toMultiMap(iterable, element -> entry(function.apply(element), element));
+ }
+
+ public static <T, K, V> Map<K, List<V>> toMultiMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
+ Map<K, List<V>> map = new HashMap<>();
+ for (T element : iterable) {
+ Map.Entry<K, V> pair = function.apply(element);
+ map.computeIfAbsent(pair.getKey(), key -> new ArrayList<V>())
+ .add(pair.getValue());
+ }
+ return map;
+ }
+
+ public static <K, V1, V2> Map<K, V2> eagerMapValues(Map<K, V1> map, Function<V1, V2> function) {
+ Map<K, V2> result = new HashMap<>();
+ for (Map.Entry<K, V1> element : map.entrySet()) {
+ result.put(element.getKey(), function.apply(element.getValue()));
+ }
+ return result;
+ }
}