List collection deduplication operation (code implementation and use of tool classes)

One: set A and set B, where set A contains set B

 backupMapYes contains all objects of 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                 }

 

Second, the collection remove 

If it is a collection remove, it can only be removed by index.

Three, set A and object B, where set A contains object 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         }

 

Four: Set A and set B, where A has B, and B has A. Now I want to delete the duplicate objects from A.

A is idsassets, B is 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             }

 

 Five, the use of the tool class 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         }

 

 

result:

 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]
10Complement of Intersection disjunctionst.toString()Students [name= aa6]
 11 Complement of Intersection disjunctionst.toString()Students [name= aa5]
 12 Complement of Intersection disjunctionst.toString()Students [name= aa4]
 13 Complement of Intersection Complement disjunction2 st.toString()Students [name= aa6]
 Complement of 14 intersection disjunction2 st.toString()Students [name= aa5]
 Complement of 15 intersection disjunction2 st.toString()Students [name= aa4]
 16 list1 Difference with list2 subtract st.toString()Students [name= aa4]
 17 Difference between list1 and list2 subtract st.toString()Students [name= aa5]
 18 Difference between list2 and list1 subtract2 st.toString()Students [name= aa6]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602739&siteId=291194637