[Java] [27] List of two union / non-union repeat / intersection / difference set

text:

Two existing List

List<String> list1 =new ArrayList<String>();
list1.add("A");
list1.add("B);

List<String> list2 =new ArrayList<String>();
list2.add("B");
list2.add("C");

1, and set

Note: If you do not want to change the value of the original list, you can build a temporary list to transition data, List temList = list1.clone ();  // clone objects

list1.addAll (List2);
 // Run Results: A, B, B, C

2, and no duplicate set

list2.removeAll (List1); 
list1.addAll (List2); 
// Run Results: A, B, C

3, intersection

list1.retainAll (List2);
 // Run Results: B

4, difference

list1.removeAll (List2);
 // Run Results: A

Reference blog:

java list two intersection and set difference set to repeat and set - CSDN blog - n_meng's blog
https://blog.csdn.net/n_meng/article/details/71622972

Guess you like

Origin www.cnblogs.com/huashengweilong/p/10965367.html