From fe190d8f432cc7358c244ec246563cde2bf68c81 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 2 Mar 2024 23:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=9B=86=E5=90=88=E8=BD=AC?= =?UTF-8?q?=20Map=20=E7=9A=84=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=B5=81=E5=8D=B3=E5=8F=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/collection/CollectionTools.java | 124 ------------------ .../plusone/commons/util/TreeBuilder.java | 5 +- 2 files changed, 2 insertions(+), 127 deletions(-) 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());