diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java index 2ca85f6..10b1819 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/CollectionTools.java @@ -1,17 +1,10 @@ package xyz.zhouxy.plusone.commons.collection; -import java.util.Arrays; import java.util.Collection; -import java.util.Comparator; -import java.util.HashMap; import java.util.Map; -import java.util.TreeMap; -import java.util.function.Function; import javax.annotation.Nullable; -import com.google.common.base.Preconditions; - public class CollectionTools { // isEmpty @@ -34,123 +27,6 @@ public class CollectionTools { return map != null && !map.isEmpty(); } - // Collection -> Map - - public static HashMap toHashMap( // NOSONAR return implementation - Iterable c, - Function keyGenerator, - int initialCapacity) { - HashMap map = new HashMap<>(initialCapacity); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static HashMap toHashMap( // NOSONAR return implementation - Collection c, - Function keyGenerator) { - return toHashMap(c, keyGenerator, c.size()); - } - - public static SafeConcurrentHashMap toConcurrentHashMap( - Iterable c, - Function keyGenerator, - int initialCapacity) { - SafeConcurrentHashMap map = new SafeConcurrentHashMap<>(initialCapacity); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static SafeConcurrentHashMap toConcurrentHashMap( - Collection c, - Function keyGenerator) { - return toConcurrentHashMap(c, keyGenerator, c.size()); - } - - public static , V> TreeMap toTreeMap( // NOSONAR return implementation - Iterable c, - Function keyGenerator) { - TreeMap map = new TreeMap<>(); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static TreeMap toTreeMap( // NOSONAR return implementation - Iterable c, - Function keyGenerator, - Comparator keycComparator) { - TreeMap map = new TreeMap<>(keycComparator); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static void fillIntoEmptyMap( - Map map, - Iterable c, - Function keyGenerator) { - Preconditions.checkNotNull(map); - Preconditions.checkNotNull(c); - Preconditions.checkNotNull(keyGenerator); - Preconditions.checkArgument(map.isEmpty(), "The map should be empty."); - for (V v : c) { - map.put(keyGenerator.apply(v), v); - } - } - - // array -> map - - public static HashMap toHashMap( // NOSONAR return implementation - V[] c, - Function keyGenerator, - int initialCapacity) { - HashMap map = new HashMap<>(initialCapacity); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static HashMap toHashMap( // NOSONAR return implementation - V[] c, - Function keyGenerator) { - return toHashMap(c, keyGenerator, c.length); - } - - public static SafeConcurrentHashMap toConcurrentHashMap( - V[] c, - Function keyGenerator, - int initialCapacity) { - SafeConcurrentHashMap map = new SafeConcurrentHashMap<>(initialCapacity); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static SafeConcurrentHashMap toConcurrentHashMap( - V[] c, - Function keyGenerator) { - return toConcurrentHashMap(c, keyGenerator, c.length); - } - - public static , V> TreeMap toTreeMap( // NOSONAR return implementation - V[] c, - Function keyGenerator) { - TreeMap map = new TreeMap<>(); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static TreeMap toTreeMap( // NOSONAR return implementation - V[] c, - Function keyGenerator, - Comparator keyComparator) { - TreeMap map = new TreeMap<>(keyComparator); - fillIntoEmptyMap(map, c, keyGenerator); - return map; - } - - public static void fillIntoEmptyMap( - Map map, V[] c, - Function keyGenerator) { - fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator); - } - private CollectionTools() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java index 6be4bfe..1caa73a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java @@ -8,8 +8,6 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; -import xyz.zhouxy.plusone.commons.collection.CollectionTools; - public class TreeBuilder { private final Function identityGetter; private final Function> parentIdentityGetter; @@ -23,7 +21,8 @@ public class TreeBuilder { } public List buildTree(Collection nodes) { - Map identityNodeMap = CollectionTools.toHashMap(nodes, identityGetter); + Map identityNodeMap = nodes.stream() + .collect(Collectors.toMap(identityGetter, Function.identity(), (n1, n2) -> n1)); List result = nodes.stream() .filter(node -> !this.parentIdentityGetter.apply(node).isPresent()) .collect(Collectors.toList());