【工具类】常用的工具类——CollectionUtil

目录

cn.hutool.core.collection.CollectionUtil

集合创建

java 复制代码
List<User> userList = CollectionUtil.list(true);
//true:ArrayList,false:LinkedList

List<String> list1 = Arrays.asList("1", "2", "3", "4", "5");//不可变集合
List<String> list2 = CollectionUtil.toList("1", "2", "3", "4", "5");//可变集合
System.out.println(list2);//[1, 2, 3, 4,5]

集合清空

java 复制代码
List<String> list = CollectionUtil.toList("1", "2", "3", "4", "5");
CollectionUtil.clear(list);
System.out.println(list);//[]

集合判空

java 复制代码
List<String> list1 = null;
List<String> list2 = new ArrayList<>();
boolean isEmpty = CollectionUtils.isEmpty(list1); // true
boolean isEmpty = CollectionUtils.isEmpty(list2); // true

List<String> list = Arrays.asList("a", "b", "c");
boolean isNotEmpty = CollectionUtils.isNotEmpty(list); // true

List<String> list1 = null;
List<String> list2 = Arrays.asList("1","2","3");
List<String> result1 = CollectionUtil.emptyIfNull(list1);//[]
List<String> result2 = CollectionUtil.emptyIfNull(list2);//["1","2","3"]

Set<String> set1 = null;
Set<String> set2 = new HashSet<>(Arrays.asList("1","2","3"));
Set<String> result3 = CollectionUtil.emptyIfNull(set1);//[]
Set<String> result4 = CollectionUtil.emptyIfNull(set2);//["1","2","3"]

//空集合给默认值
List<String> list = new ArrayList<>();
List<String> result = CollectionUtil.defaultIfEmpty(list, Arrays.asList("1", "2", "3"));//["1", "2", "3"]

//判断是否有空元素
CollectionUtil.hasNull(list);

集合去重

java 复制代码
List<String> list = Arrays.asList("a", "b", "a", "c");
List<String> distinctList = CollectionUtil.distinct(list); // ["a", "b", "c"]

集合过滤

java 复制代码
//List<String> list =Arrays.asList("a", "b", "c");//Arrays.asList()返回的集合是不可变长度的,在这里这么用会报错:UnsupportedOperationException
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> filteredList = CollectionUtil.filter(list, e -> e.equals("a")); // ["a"]

集合转换

java 复制代码
List<String> list = Arrays.asList("1", "2", "3");
List<Integer> mappedList = CollectionUtil.map(list, Integer::valueOf,true); // [1, 2, 3]

集合合并

java 复制代码
List<String> list1 = Arrays.asList("a", "b");
List<String> list2 = Arrays.asList("b", "c");

Set<String> unionDistinctList = CollectionUtil.unionDistinct(list1, list2); // ["a", "b", "c"]

List<String> unionAllList = (List<String>)CollectionUtil.unionAll(list1, list2); // ["a", "b", "c"]

集合交集

java 复制代码
List<String> list1 = Arrays.asList("a", "b");
List<String> list2 = Arrays.asList("b", "c");
List<String> intersectionList = (List<String>)CollectionUtil.intersection(list1, list2); // ["b"]

集合差集

java 复制代码
List<String> list1 = Arrays.asList("a", "b");
List<String> list2 = Arrays.asList("b", "c");
List<String> disjunctionList = (List<String>)CollectionUtil.disjunction(list1, list2); // ["a", "c"]

集合是否包含元素

java 复制代码
List<String> list = Arrays.asList("a", "b", "c");
boolean contains = CollectionUtil.contains(list, "a"); // true

List<String> list2 = Arrays.asList("a", "b", "c");
List<String> subList = Arrays.asList("a", "b");
boolean containsAll = CollectionUtil.containsAll(list2, subList); // true

集合是否包含指定元素(自定义条件)

java 复制代码
List<String> list = Arrays.asList("a", "b", "c");
boolean contains = CollectionUtil.contains(list, e -> e.equals("a")); // true

集合分页

java 复制代码
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
List<String> pageList = CollectionUtil.page(1, 2,list); // ["c", "d"],页码索引从0开始

集合分组

java 复制代码
List<User> list = Arrays.asList(
    new User("张三", 18, 1),
    new User("李四", 18, 2),
    new User("王五", 18, 3),
    new User("赵六", 18, 4),
    new User("老七", 18, 1),
    new User("勾八", 18, 1),
    new User("孙九", 18, 3),
    new User("郑十", 18, 2),
    new User("root", 18, 2));
List<List<User>> groupedList = Co
System.out.println(groupedList);
//[[User(name=张三, age=18, grade=1), User(name=老七, age=18, grade=1), User(name=勾八, age=18, grade=1)], 
//[User(name=李四, age=18, grade=2), User(name=郑十, age=18, grade=2), User(name=root, age=18, grade=2)], 
//[User(name=王五, age=18, grade=3), User(name=孙九, age=18, grade=3)], 
//[User(name=赵六, age=18, grade=4)]]

集合转字符串

java 复制代码
List<String> list = Arrays.asList("a", "b", "c");
String joinedString = CollectionUtil.join(list, ","); // "a,b,c"

元素添加

java 复制代码
List<String> list = new ArrayList<>(Arrays.asList("1", "2"
CollectionUtil.addAll(list,"4");
CollectionUtil.addAll(list,new String[]{"5","6"});
CollectionUtil.addAll(list,Arrays.asList("7","8"));
CollectionUtil.addAll(list,new HashSet<>(Arrays.asList("9"
System.out.println(list);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3"));
CollectionUtil.addAllIfNotContains(list, Arrays.asList("1", "2", "4"));
System.out.println(list);//[1, 2, 3, 4]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3"));
CollectionUtil.addIfAbsent(list,"2");//已存在不添加
CollectionUtil.addIfAbsent(list,"4");
System.out.println(list);//[1, 2, 3, 4]

元素删除

java 复制代码
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","","4",null,"5"));
CollectionUtil.removeEmpty(list);
//CollectionUtil.removeBlank(list);
System.out.println(list);//[1, 2, 3, 4,5]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","4","3","5","3","2"));
CollectionUtil.removeAny(list, "3","2");
System.out.println(list);//[1, 4,5]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","","4",null,"5"));
CollectionUtil.removeNull(list);
System.out.println(list);//[1, 2, 3, 4,5]

//自定义条件
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","","4",null,"5"));
CollectionUtil.removeWithAddIf(list, item -> item == null || item.isEmpty());
System.out.println(list);//[1, 2, 3, 4,5]

根据属性转Map

java 复制代码
List<User> list = Arrays.asList(
    new User(1,"张三", 18, 1),
    new User(2,"李四", 18, 2),
    new User(3,"王五", 18, 3),
    new User(4,"张三", 18, 4),
    new User(5,"李四", 18, 1));
Map<String, User> result = CollectionUtil.fieldValueMap(list, "id");

Map<String, User> result2 = CollectionUtil.fieldValueAsMap(list, "id", "name");

获取元素

java 复制代码
List<User> list = Arrays.asList(
            new User(1,"张三", 18, 1),
            new User(2,"李四", 18, 2),
            new User(3,"王五", 18, 3),
            new User(4,"张三", 18, 4),
            new User(5,"李四", 18, 1));
User result = CollectionUtil.get(list,6);//自带判空效果
User result = CollectionUtil.getFirst(list);
User result = CollectionUtil.getLast(list);
List<User> result = CollectionUtil.getAny(list, 0,  2);
List<Object> result = CollectionUtil.getFieldValues(list, "name");//[张三, 李四, 王五, 张三, 李四]  User类要先实现Iterable<E>接口

//截取集合
List<User> result = CollectionUtil.sub(list, 0, 3);

获取最大/最小元素

java 复制代码
List<User> list = Arrays.asList(
    new User(1,"张三", 19, 1),
    new User(2,"李四", 18, 2),
    new User(3,"王五", 16, 3),
    new User(4,"张三", 24, 4),
    new User(5,"李四", 17, 1));
//需要User先实现Comparable<e>接口
User result = CollectionUtil.max(list);//张三
User result = CollectionUtil.min(list);//王五

集合排序

java 复制代码
List<User> list = Arrays.asList(
    new User(1,"张三", 19, 1),
    new User(2,"李四", 18, 2),
    new User(3,"王五", 16, 3),
    new User(4,"张三", 24, 4),
    new User(5,"李四", 17, 1));
//不需要User先实现Comparable<e>接口,默认从小到大排序
List<User> result = CollectionUtil.sort(list, Comparator.comparing(User::getAge));
相关推荐
x***13398 小时前
MySQL 篇 - Java 连接 MySQL 数据库并实现数据交互
java·数据库·mysql
xuanzdhc8 小时前
Gitgit
java·linux·运维·服务器·c++·git
无心水8 小时前
【Python实战进阶】7、Python条件与循环实战详解:从基础语法到高级技巧
android·java·python·python列表推导式·python条件语句·python循环语句·python实战案例
一点★8 小时前
“equals”与“==”、“hashCode”的区别和使用场景
java·开发语言
N***H4868 小时前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
0***v7778 小时前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
s***w1129 小时前
SpringMVC新版本踩坑[已解决]
java
老李头喽9 小时前
走进单元测试
java·单元测试
就叫飞六吧9 小时前
Spring MVC 接口命名为什么会有 *.do/actions等身影?
java·spring·mvc
葡萄成熟时 !9 小时前
黑马学生管理系统
java·开发语言