Apache-Commons-Collections 使用笔记

Apache-Commons-Collections 使用笔记

Apache Commons Collections 官网 https://commons.apache.org/proper/commons-collections/

Apache Commons Collections 4.2 官方API文档 https://commons.apache.org/proper/commons-collections/javadocs/api-release/index.html

MapUtils

package org.apache.commons.collections4; public class MapUtils

isNotEmpty() 集合非空

public static boolean isNotEmpty(Map<?,?> map)

isEmpty() 集合判空

public static boolean isEmpty(Map<?,?> map)

CollectionUtils

isNotEmpty 集合判空

CollectionUtils.isNotEmpty()

判断集合只包含空元素

用 cn.hutool.core.collection.removeBlank 先删除 blank 元素,再判空,注意 removeBlank 会直接修改原集合

CollectionUtils.isEmpty(CollectionUtil.removeBlank(col))

MultiValuedMap

一个 key 中可以存放多个 value 的 Map,类似 Spring 的 MultiValueMap

@Test
public void test() {
    MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
    String key1 = "key1";
    map.put(key1, "A");
    map.put(key1, "B");
    map.put(key1, "C");
    Collection<String> values = map.get(key1);
    Assertions.assertEquals(3, values.size());
    System.out.println(map);
}