Java基础类(七):CollectionUtils工具类
2021/9/20 14:07:05
本文主要是介绍Java基础类(七):CollectionUtils工具类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
1、Pom包
2、常用方法
2.1、集合判断
2.2、并集
2.3、交集
2.4、交集的补集(析取)
2.5、差集
2.6、不可修改的集合【已经过期】
1、Pom包
从maven找到最新jar包如下:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.3</version> </dependency>
2、常用方法
2.1、集合判断
判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): true CollectionUtils.isEmpty({a,b}): false 判断集合是否不为空: CollectionUtils.isNotEmpty(null): false CollectionUtils.isNotEmpty(new ArrayList()): false CollectionUtils.isNotEmpty({a,b}): true
源码分析
public class CollectionUtils { public static boolean isEmpty(Collection<?> coll) { return coll == null || coll.isEmpty(); } public boolean isEmpty() { return size == 0; } }
2.2、并集
public class TestCollectionUtils { public static void main(String[] args) { String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2个数组取并集 System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB))); //[A, B, C, D, E, F, G, H, K] } }
2.3、交集
public class TestCollectionUtils { public static void main(String[] args) { String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2个数组取交集 System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB))); //[B, D, F] } }
2.4、交集的补集(析取)
public class TestCollectionUtils { public static void main(String[] args) { String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2个数组取交集 的补集 System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB))); //[A, C, E, G, H, K] } }
2.5、差集
public class TestCollectionUtils { public static void main(String[] args) { String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //arrayA扣除arrayB System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB))); //[A, C, E] } }
2.6、不可修改的集合【已经过期】
public class TestCollectionUtils { public static void main(String[] args) { Collection<String> c = new ArrayList<>(); //deprecated Collection<String> s = CollectionUtils.unmodifiableCollection(c); c.add("a"); c.add("b"); c.add("c"); //Exception in thread "main" java.lang.UnsupportedOperationException //s.add("have a error"); System.out.println(s); } }
这篇关于Java基础类(七):CollectionUtils工具类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略