In the Java Collection framework, there are three similar methods, addall (), retainall () and removeall (). addall (), the retainall (), and The removeall () methods are equivalent to the set theoretic union, intersection, and complement operators, respectively. these are already strated in the following picture.
In my local machine, just for these methods, I made a test for them respectively.
Package COM. alberta Shao. DS. set; import static Org. JUnit. assert. assertequals; import Java. util. arraylist; import Java. util. arrays; import Java. util. collections; import Java. util. list; import Org. JUnit. after; import Org. JUnit. test; public class testcollectionsmethods {list <string> lista = NULL; List <string> listb = NULL; List <string> result = new arraylist <string> (); list <string> testlist = new arraylist <string> (); @ Testpublic void testaddall () {lista = arrays. aslist ("apple", "blanana", "grape"); listb = arrays. aslist ("apple", "orange", "Pear"); collections. addall (testlist, "apple", "blanana", "grape", "apple", "orange", "Pear"); result. addall (lista); result. addall (listb); assertequals (testlist, result) ;}@ testpublic void testretainall () {lista = arrays. aslist ("apple", "blanana", "grape"); listb = arrays. aslist ("apple", "ora Nge "," Pear "); collections. addall (testlist, "apple"); result. addall (lista); result. retainall (listb); assertequals (testlist, result) ;}@ testpublic void testremoveall () {lista = arrays. aslist ("apple", "blanana", "grape"); listb = arrays. aslist ("apple", "orange", "Pear"); collections. addall (testlist, "blanana", "grape"); result. addall (lista); result. removeall (listb); assertequals (testlist, result) ;}@ afterpublic void t Estprint () {system. Out. println ("-----------------------"); If (result! = NULL &&! Result. isempty () {for (string STR: result) {system. Out. println (STR) ;}} system. Out. println ("-----------------------");}}
The result of execution is as follows:
Hope it's beneficial to you