欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 【工具类】常用的工具类——CollectionUtil

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

2025/5/30 10:37:44 来源:https://blog.csdn.net/weixin_37833693/article/details/148166620  浏览:    关键词:【工具类】常用的工具类——CollectionUtil

目录

    • cn.hutool.core.collection.CollectionUtil
      • 集合创建
      • 集合清空
      • 集合判空
      • 集合去重
      • 集合过滤
      • 集合转换
      • 集合合并
      • 集合交集
      • 集合差集
      • 集合是否包含元素
      • 集合是否包含指定元素(自定义条件)
      • 集合分页
      • 集合分组
      • 集合转字符串
      • 元素添加
      • 元素删除
      • 根据属性转Map
      • 获取元素
      • 获取最大/最小元素
      • 集合排序

cn.hutool.core.collection.CollectionUtil

集合创建

List<User> userList = CollectionUtil.list(true);
//true:ArrayList,false:LinkedListList<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]

集合清空

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

集合判空

List<String> list1 = null;
List<String> list2 = new ArrayList<>();
boolean isEmpty = CollectionUtils.isEmpty(list1); // true
boolean isEmpty = CollectionUtils.isEmpty(list2); // trueList<String> list = Arrays.asList("a", "b", "c");
boolean isNotEmpty = CollectionUtils.isNotEmpty(list); // trueList<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);

集合去重

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

集合过滤

//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"]

集合转换

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

集合合并

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"]

集合交集

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

集合差集

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"]

集合是否包含元素

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

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

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

集合分页

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

集合分组

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)]]

集合转字符串

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

元素添加

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]

元素删除

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

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");

获取元素

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);

获取最大/最小元素

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);//王五

集合排序

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));

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词