List集合去重复的操作(代码实现以及工具类的使用)
2018-04-18 14:12
603 查看
一:集合A和集合B,其中集合A是包含集合B的
backupMapYes中全部包含backupNoList的对象
1 Iterator<MerchantBrand> iterator = backupMapYes.iterator(); 2 while (iterator.hasNext()) { 3 MerchantBrand merchantBrand = iterator.next(); 4 for (String name : backupNoList) { 5 if (StringUtils.isNotEmpty(name) 6 && name.equalsIgnoreCase(merchantBrand 7 .getBrandNameZh())) { 8 iterator.remove(); 9 } 10 } 11 }
二,集合remove
假如是集合remove的话只能按照索引移除。
三,集合A和对象B,其中集合A是包含对象B的
1 Iterator<User> it = list.iterator(); 2 while (it.hasNext()) 3 { 4 User userObj = it.next(); 5 if (userObj.getId() == 3) 6 { 7 it.remove(); 8 } 9 } 10 //剩下的用户 11 System.err.println("剩下的用户:"); 12 for (User result : list) 13 { 14 System.err.println("id:" + result.getId() + "\tname:" + result.getName()); 15 }
四:集合A和集合B,其中A中有B,B中有A,现在想把重复的对象从A中删除。
A是idsassets,B是idsassetsBackup
1 Collection<Long> ret = CollectionUtils.intersection(idsassets, 2 idsassetsBackup); 3 Iterator<Long> iterator = idsassets.iterator(); 4 while (iterator.hasNext()) { 5 Long id = iterator.next(); 6 for (Long name : ret) { 7 if (String.valueOf(name).equalsIgnoreCase( 8 String.valueOf(id))) { 9 iterator.remove(); 10 } 11 } 12 }
五,工具类CollectionUtils的使用
1 Students st1=new Students(); 2 Students st2=new Students(); 3 Students st3=new Students(); 4 Students st4=new Students(); 5 Students st5=new Students(); 6 Students st6=new Students(); 7 List<Students> list1=new ArrayList<Students>(); 8 List<Students> list2=new ArrayList<Students>(); 9 st1.setName("aa1"); 10 st2.setName("aa2"); 11 st3.setName("aa3"); 12 st4.setName("aa4"); 13 st5.setName("aa5"); 14 st6.setName("aa6"); 15 list1.add(st1); 16 list1.add(st2); 17 list1.add(st3); 18 list1.add(st4); 19 list1.add(st5); 20 /*list1.add(st6);*/ 21 list2.add(st1); 22 list2.add(st2); 23 list2.add(st3); 24 list2.add(st6); 25 Collection<Students> union = CollectionUtils.union(list1, list2); 26 for(Students st:union) { 27 System.err.println( " 并集union<<<<<<st.toString()"+st.toString()); 28 } 29 Collection<Students> intersection = CollectionUtils.intersection(list1, list2); 30 for(Students st:intersection) { 31 System.err.println( " 交集intersectionst.toString()"+st.toString()); 32 } 33 Collection<Students> disjunction = CollectionUtils.disjunction(list1, list2); 34 for(Students st:disjunction) { 35 System.err.println( "交集的补集 disjunctionst.toString()"+st.toString()); 36 } 37 Collection<Students> disjunction2 = CollectionUtils.disjunction(list2, list1); 38 for(Students st:disjunction2) { 39 System.err.println( "交集的补集disjunction2 st.toString()"+st.toString()); 40 } 41 Collection<Students> subtract = CollectionUtils.subtract(list1, list2); 42 for(Students st:subtract) { 43 System.err.println( "list1与list2的差subtract st.toString()"+st.toString()); 44 } 45 Collection<Students> subtract2 = CollectionUtils.subtract(list2, list1); 46 for(Students st:subtract2) { 47 System.err.println( "list2与list1的差subtract2 st.toString()"+st.toString()); 48 } 49 }
结果:
1 并集union<<<<<<st.toString()Students [name=aa3] 2 并集union<<<<<<st.toString()Students [name=aa6] 3 并集union<<<<<<st.toString()Students [name=aa5] 4 并集union<<<<<<st.toString()Students [name=aa4] 5 并集union<<<<<<st.toString()Students [name=aa2] 6 并集union<<<<<<st.toString()Students [name=aa1] 7 交集intersectionst.toString()Students [name=aa3] 8 交集intersectionst.toString()Students [name=aa2] 9 交集intersectionst.toString()Students [name=aa1] 10 交集的补集 disjunctionst.toString()Students [name=aa6] 11 交集的补集 disjunctionst.toString()Students [name=aa5] 12 交集的补集 disjunctionst.toString()Students [name=aa4] 13 交集的补集disjunction2 st.toString()Students [name=aa6] 14 交集的补集disjunction2 st.toString()Students [name=aa5] 15 交集的补集disjunction2 st.toString()Students [name=aa4] 16 list1与list2的差subtract st.toString()Students [name=aa4] 17 list1与list2的差subtract st.toString()Students [name=aa5] 18 list2与list1的差subtract2 st.toString()Students [name=aa6]
相关文章推荐
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- Java 取集合的差集,list集合或者map集合,都可以取差集。使用guava工具类来做。而不是重复的造轮子。
- Java基础知识强化之集合框架笔记29:使用LinkedList实现栈数据结构的集合代码(面试题)
- 利用hashtable模拟实现权限验证(同map思想-->只能有一个用户名,可重复密码)以及增删查改操作
- 【转】二叉树的实现及操作以及指针使用和删除注意事项
- .net使用Aspose.Words进行Word替换操作的实现代码
- [转]在Java中实现.net中DataTable功能以及操作双数据库的List连接问题解决方案探究
- 在Java中实现.net中DataTable功能以及操作双数据库的List连接问题解决方案探究
- 使用C语言实现链栈以及initialize,push,pop,isEmpty,getlength,destory等操作
- 去除List集合中的重复对象,Map遍历代码
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
- 第十七天3月12日之一句代码实现crud操作(手动写的,没使用其他jar包和框架)
- 市面上所有号称"虚拟机","防火墙"的实时监控杀毒软件无一不是使用的IFSHOOK技术.但是同时也有一些朋友不断写MAIL给我打听如何实现读写的监控.下面给出用VTOOLSD写的代码.也就是所有实时杀毒软件的奥秘.同时,很多拦截文件操作的软件,例如对目录加
- 操作集合的工具类:Collections使用示例
- 利用Collections工具类来操作List集合
- Python代码实现:删除一个list里面的重复元素
- 一对多映射关系中使用List集合的写法以及List下标由谁维护
- 使用jQuery操作Cookies的实现代码
- 设计一个整型链表类list,能够实现链表节点的插入、删除、以及链表数据的输出操作。
- java 策略模式,list集合,实现id 姓名年龄正序倒序排序(如果年龄或者姓名重复,按id正序排序)