場景
Java開發中常用的CollectionUtils
一、Spring Framework的CollectionUtils
包路徑:org.springframework.util.CollectionUtils
核心方法:
isEmpty(Collection<?> coll)
List<String> list = null;
boolean empty = CollectionUtils.isEmpty(list);? // true
containsAny(Collection<?> source, Collection<?> candidates)
boolean hasCommon = CollectionUtils.containsAny(List.of(1,2),? Set.of(2,3));? // true
二、Apache Commons Collections的CollectionUtils
包路徑:org.apache.commons.collections4.CollectionUtils
特色方法:
isEqualCollection(Collection<?> a, Collection<?> b)
boolean equal = CollectionUtils.isEqualCollection(List.of("A",? "B", "B"),List.of("B",? "A", "B")
); // true(忽略順序)
collate(Iterable<? extends T> a, Iterable<? extends T> b, Comparator<? super T> c)
List<Integer> merged = CollectionUtils.collate(List.of(1,? 3),List.of(2,? 4),Comparator.naturalOrder()
); // [1,2,3,4]
三、Google Guava的Collections2
包路徑:com.google.common.collect.Collections2
高階用法:
filter(Collection<E>, Predicate<? super E>)
Collection<String> filtered = Collections2.filter(Lists.newArrayList("a",? "b", null),Objects::nonNull
); // ["a", "b"]
transform(Collection<F>, Function<? super F, ? extends T>)
Collection<Integer> lengths = Collections2.transform(List.of("aa",? "bbb"),String::length
); // [2, 3]
Java工具庫Guava的集合工具類Iterables、Lists、Sets、Maps、Multisets、Multimaps的常用方法示例:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/127926429
四、Hutool的CollUtil
包路徑:cn.hutool.core.collection.CollUtil
國產工具亮點:
newArrayList(E... elements)
List<String> list = CollUtil.newArrayList("a",? "b"); // 可變列表
groupByField(Collection<T>, String fieldName)
List<User> users = Arrays.asList(new? User(1), new User(2));
Map<Integer, List<User>> group = CollUtil.groupByField(users,? "id");
五、Java標準庫的Collections
基礎工具類:
synchronizedCollection(Collection<T> c)
Collection<String> syncColl = Collections.synchronizedCollection(new? ArrayList<>());
unmodifiableCollection(Collection<? extends T> c)
Collection<Integer> immutable = Collections.unmodifiableCollection(List.of(1,2));
工具類對比選擇指南
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
實現
Spring Framework中CollectionUtils工具類的核心方法解析及使用示例
一、集合判空操作
??????? List<String> list = new ArrayList<>();boolean empty = CollectionUtils.isEmpty(list);?System.out.println(empty);Map<String, Object> map = Collections.singletonMap("key",? "value");boolean notEmpty = CollectionUtils.isEmpty(map);?System.out.println(notEmpty);
二、集合合并與轉換
mergeArrayIntoCollection()
?????? String[] arr = {"a", "b"};List<String> target = new ArrayList<>(List.of("c"));CollectionUtils.mergeArrayIntoCollection(arr,? target);System.out.println(target);
toIterator()
Vector<String> vector = new Vector<>(Set.of("x", "y"));Enumeration<String> en = vector.elements();Iterator<String> it = CollectionUtils.toIterator(en);while (it.hasNext()){System.out.println(it.next());}
三、條件過濾與查找
findFirstMatch()
??????? List<Integer> source = List.of(1,? 2, 3);List<Integer> candidates = List.of(3,? 4);Integer match = CollectionUtils.findFirstMatch(source,? candidates); // 3System.out.println(match);
containsAny()
??????? boolean hasCommon = CollectionUtils.containsAny(List.of("A",? "B"),Set.of("B",? "C")); // trueSystem.out.println(hasCommon);boolean hasCommon1 = CollectionUtils.containsAny(List.of("A",? "B"),Set.of("D",? "C")); // falseSystem.out.println(hasCommon1);
四、特殊集合操作
hasUniqueObject()
??????? List<Object> singleton = Collections.singletonList("unique");boolean isUnique = CollectionUtils.hasUniqueObject(singleton);? // trueSystem.out.println(isUnique);
toMultiValueMap()
??????? //將一個鍵值對的集合轉換成一個 MultiValueMap,其中每個鍵可以映射到多個值。這對于處理例如表單提交數據、查詢參數等場景特別有用。List<Pair<String, Integer>> pairs = List.of(Pair.of("a",? 1),Pair.of("a",? 2));Map<String, List<Integer>> tempMap = new HashMap<>();pairs.forEach(pair? ->tempMap.computeIfAbsent(pair.getKey(),? k -> new ArrayList<>()).add(pair.getValue()));MultiValueMap<String, Integer> map1 = CollectionUtils.toMultiValueMap(tempMap);System.out.println(map1);