Requires a collection of 2 sets.
Set set, as follows
1 ImportJava.util.HashSet;2 ImportJava.util.Set;3 4 Public classSettest {5 Public Static voidMain (string[] args) {6Set<string> set1=NewHashset<string>();7Set<string> set2=NewHashset<string>();8Set<string> result=NewHashset<string>();9 TenSet1.add ("a"); OneSet1.add ("B"); ASet1.add ("C"); -Set1.add ("E"); -Set1.add ("D"); the - -Set2.add ("A1"); -Set2.add ("B1"); +Set2.add ("C1"); -Set2.add ("E"); +Set2.add ("D"); A at result.clear (); - Result.addall (SET1); - Result.retainall (Set2); -System.out.println ("Intersection:" +result); - - result.clear (); in Result.addall (SET1); - Result.removeall (Set2); toSYSTEM.OUT.PRINTLN ("Difference set:" +result); + - result.clear (); the Result.addall (SET1); * Result.addall (Set2); $System.out.println ("and set:" +result);Panax Notoginseng - } the}
The list collection method is the same as the code below
1 Importjava.util.ArrayList;2 Importjava.util.List;3 4 Public classListtest {5 Public Static voidMain (string[] args) {6List<string> list1=NewArraylist<string>();7List<string> list2=NewArraylist<string>();8List<string> result=NewArraylist<string>();9 TenList1.add ("a"); OneList1.add ("B"); AList1.add ("C"); -List1.add ("E"); -List1.add ("D"); the - -List2.add ("A1"); -List2.add ("B1"); +List2.add ("C1"); -List2.add ("E"); +List2.add ("D"); A at result.clear (); - Result.addall (list1); - Result.retainall (list2); -System.out.println ("Intersection:" +result); - - result.clear (); in Result.addall (list1); - Result.removeall (list2); toSYSTEM.OUT.PRINTLN ("Difference set:" +result); + - result.clear (); the Result.addall (list1); * Result.addall (list2); $System.out.println ("and set:" +result);Panax Notoginseng } - the}
In addition, the return values of AddAll (), RemoveAll (), Retainall () Three methods are Boolean values, not collection types, and you need to be aware of them!
[Java] asks for a set of 2 sets