From fe49fbba588e456d27e24fc60e75de5ddf466af1 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 3 Jun 2023 23:58:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B0=86=E9=9B=86=E5=90=88?= =?UTF-8?q?=E6=88=96=E6=95=B0=E7=BB=84=E9=87=8C=E7=9A=84=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=A1=AB=E5=85=85=E5=88=B0=20Map=20=E4=B8=AD=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/MoreCollections.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java index f63f987..5ad4142 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreCollections.java @@ -1,18 +1,128 @@ package xyz.zhouxy.plusone.commons.util; +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 xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; + public class MoreCollections { + + // isEmpty + public static boolean isEmpty(@Nullable Collection collection) { return collection == null || collection.isEmpty(); } + public static boolean isEmpty(@Nullable Map map) { + return map == null || map.isEmpty(); + } + + // isNotEmpty + public static boolean isNotEmpty(@Nullable Collection collection) { return collection != null && !collection.isEmpty(); } + public static boolean isNotEmpty(@Nullable Map map) { + return map != null && !map.isEmpty(); + } + + // Collection -> Map + + public static HashMap toHashMap(Iterable c, Function keyGenerator, + int initialCapacity) { + HashMap map = new HashMap<>(initialCapacity); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static HashMap toHashMap(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 TreeMap toTreeMap(Iterable c, Function keyGenerator) { + TreeMap map = new TreeMap<>(); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static TreeMap toTreeMap(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) { + if (map == null || !map.isEmpty()) { + throw new IllegalArgumentException("The map should be empty."); + } + for (V v : c) { + map.put(keyGenerator.apply(v), v); + } + } + + // array -> map + + public static HashMap toHashMap(V[] c, Function keyGenerator, int initialCapacity) { + HashMap map = new HashMap<>(initialCapacity); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static HashMap toHashMap(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 TreeMap toTreeMap(V[] c, Function keyGenerator) { + TreeMap map = new TreeMap<>(); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static TreeMap toTreeMap(V[] c, Function keyGenerator, + Comparator keycComparator) { + TreeMap map = new TreeMap<>(keycComparator); + fillIntoEmptyMap(map, c, keyGenerator); + return map; + } + + public static void fillIntoEmptyMap(Map map, V[] c, Function keyGenerator) { + fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator); + } + private MoreCollections() { throw new IllegalStateException("Utility class"); }