CollectionUtils工具类的使用
来源:互联网 发布:北京壁虎网络 编辑:程序博客网 时间:2024/05/20 11:19
CollectionUtils在真实项目中,是一个非常好用的工具类,使用非常频繁。它可以使代码更加简洁和安全。刚好在工作中利用这个工具类重构代码,顺便总结下分享分享:
并集
@Testpublic void testUnion(){ 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]}
交集
@Testpublic void testIntersection(){ 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]}
交集的补集(析取)
@Testpublic void testDisjunction(){ 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]}
差集(扣除)
@Testpublic void testSubtract(){ 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]}
集合是否为空
@Testpublic void testIsEmpty(){ class Person{} class Girl extends Person{} List<Integer> first = new ArrayList<>(); List<Integer> second = null; List<Person> boy = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy.add(new Girl()); //判断集合是否为空 System.out.println(CollectionUtils.isEmpty(first)); //true System.out.println(CollectionUtils.isEmpty(second)); //true System.out.println(CollectionUtils.isEmpty(boy)); //false //判断集合是否不为空 System.out.println(CollectionUtils.isNotEmpty(first)); //false System.out.println(CollectionUtils.isNotEmpty(second)); //false System.out.println(CollectionUtils.isNotEmpty(boy)); //true}
集合是否相等
@Testpublic void testIsEqual(){ class Person{} class Girl extends Person{ } List<Integer> first = new ArrayList<>(); List<Integer> second = new ArrayList<>(); first.add(1); first.add(2); second.add(2); second.add(1); Girl goldGirl = new Girl(); List<Person> boy1 = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy1.add(new Girl()); List<Person> boy2 = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy2.add(new Girl()); //比较两集合值 System.out.println(CollectionUtils.isEqualCollection(first,second)); //true System.out.println(CollectionUtils.isEqualCollection(first,boy1)); //false System.out.println(CollectionUtils.isEqualCollection(boy1,boy2)); //false List<Person> boy3 = new ArrayList<>(); //每个男孩心里都装着一个女孩 boy3.add(goldGirl); List<Person> boy4 = new ArrayList<>(); boy4.add(goldGirl); System.out.println(CollectionUtils.isEqualCollection(boy3,boy4)); //true}
不可修改的集合
我们对c进行操作,s也同样获得了和c相同的内容,这样就可以避免其他人员修改这个s对象。有时候需要对它进行保护,避免返回结果被人修改。
@Testpublic void testUnmodifiableCollection(){ Collection<String> c = new ArrayList<>(); Collection<String> s = CollectionUtils.unmodifiableCollection(c); c.add("boy"); c.add("love"); c.add("girl"); //! s.add("have a error"); System.out.println(s);}
Collections.unmodifiableCollection可以得到一个集合的镜像,它的返回结果是不可直接被改变,否则会提示错误
java.lang.UnsupportedOperationExceptionat org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)
阅读全文
0 0
- CollectionUtils工具类的使用
- CollectionUtils的使用
- apache commons collections CollectionUtils工具类简单使用
- apache commons collections CollectionUtils工具类简单使用
- apache commons collections CollectionUtils工具类简单使用
- apache commons collections CollectionUtils工具类简单使用
- apache commons collections CollectionUtils工具类简单使用
- apache commons collections CollectionUtils工具类简单使用
- 集合操作工具类CollectionUtils
- 集合操作工具类CollectionUtils
- 拓展集合工具类CollectionUtils
- apache工具类之CollectionUtils(待续更新)
- 集合的常用操作类CollectionUtils
- 集合的常用操作类CollectionUtils
- 集合的常用操作类CollectionUtils
- StringUtils类和collectionUtils的用法
- 集合的常用操作类CollectionUtils
- 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils探究
- 指针函数和函数指针
- WAL日志的命名规则深度解析(二)
- 修改密码-测试用例设计
- localStorage使用小结
- JavaScript window、location对象基础知识整理
- CollectionUtils工具类的使用
- Android 导入第三方jar时 出现ClassNotFoundException
- 《Text Mining and Analytics》学习笔记——第一周
- 1.1java介绍
- Filter 拦截器
- Android代码实现自动切换USB调试
- 12-12作业
- 1004 n^n的末位数字
- activemq使用方法