Follow the wechat official account: CodingTechWork, learn and progress together.
The introduction
We often encounter problems such as filtering data and seeking set among some collection classes. Here, we take List as an example. After all, after we get data in the database, we use List set more for operation.
The template
code
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("a");
stringList.add("b");
stringList.add("c");
stringList.add("i");
stringList.add("j");
stringList.add("a");
//
// Method 1: filter directly through retainAll
List<String> stringList1 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
stringList1.retainAll(stringList);
System.out.println("Intersection 1:" + stringList1);
// Method 2: Filter out data that exists in stringList
List<String> stringList1_2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
List<String> strings = stringList1_2.stream()
.filter(item -> stringList.contains(item))
.collect(toList());
System.out.println("Intersection 2:" + strings);
// select * from *
// There is a double union
List<String> stringList2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
stringList2.addAll(stringList);
System.out.println("Union:" + stringList2);
// No duplicate union
List<String> stringList2_2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
List<String> stringList_1 = new ArrayList<>(Arrays.asList("a,b,c,i,j,a".split(",")));
stringList2_2.removeAll(stringList_1);
stringList_1.addAll(stringList2_2);
System.out.println("No double union:" + stringList_1);
//
Method 1: Use the removeAll() method directly
List<String> stringList3 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
stringList3.removeAll(stringList);
System.out.println("Difference set 1:" + stringList3);
// Method 2: Filter out data that does not exist in stringList and then interintersect with the array
List<String> stringList3_2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(","))); stringList3_2.retainAll(stringList3_2.stream() .filter(item -> ! stringList.contains(item)) .collect(toList())); System.out.println("Difference set 2:" + stringList3_2);
SpringApplication.run(DemoApplication.class, args);
}
Copy the code
The results of
intersection1: [A, B, C] intersection2: (a, b, c) and sets: [a, b, c, d, e, f, g, h, a, b, c, I, j, a] no weight and sets: [a, b, c, I, j, a, d, e, f, g, h] difference set1: [D, E, F, g, h] difference set2: [d, e, f, g, h]Copy the code
Note the list assignment problem
If you use an assignment like list1 = list0, then list1.retainAll(list2) or list1.removeAll(list2) the value of list0 also changes. Because lis1 and list0 point to the same address. Code:
List<String> s1 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
List<String> s2 = s1;
System.out.println("S1 address and s2 are equal (same object) :" + (s1 == s2));
s2.remove("g");
s2.remove("h");
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
Copy the code
Results:
S1 address is the same as S2 address:true
s1: [a, b, c, d, e, f]
s2: [a, b, c, d, e, f]
Copy the code
So we need to use List
list1= new ArrayList<>(list0); The way code:
List<String> s1 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
List<String> s2 = new ArrayList<>(s1);
System.out.println("S1 address and s2 are equal (same object) :" + (s1 == s2));
s2.remove("g");
s2.remove("h");
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
Copy the code
Results:
S1 address is the same as S2 address:false
s1: [a, b, c, d, e, f, g, h]
s2: [a, b, c, d, e, f]
Copy the code
If the removeAll() method is not overridden by the equals () and hashCode () methods, then the removeAll() method will be invalidated.