您的位置:首页 > 编程语言

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]

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐