Before writing the C # project, it is convenient to redo the list, using List1. Union (List2). ToList () can (enumerable.union). And, if you want to remove the List2 item from the List1, it is also convenient, list2. Except (List1). ToList ()
Now write Java project, it does not have such a native method can be directly invoked, operation, a little more than a few steps.
public static void Main (string[] args) {list<string> lista = arrays.aslist ("Unicorn", "Penguin", "Koala", "
Hippo "," unicorn ");
list<string> Listb = arrays.aslist ("Swan", "Penguin", "Ape");
The Merge lists and remove duplicated entries set<string> Set = new linkedhashset<> ();
Set.addall (Lista);
Set.addall (LISTB);
System.out.println ("Merge and remove Duplicates:" + set);
Intersection list<string> intersection = new arraylist<> (lista);
Intersection.retainall (LISTB);
System.out.println ("Intersection of two lists:" + intersection);
Concat list<string> Concat = new arraylist<> (lista);
Concat.addall (LISTB);
System.out.println ("Concat of two lists:" + Concat);
Lista-listb list<string> minus = new arraylist<> (lista);
Minus.removeall (LISTB); System.out.println ("Remove items that" aRe in Listb from Lista: "+ minus"); }
Program output:
Merge and remove duplicates: [Unicorn, Penguin, Koala, Hippo, Swan, Ape]
intersection of two lists: [Pe Nguin]
Concat of two lists: [Unicorn, Penguin, Koala, Hippo, Unicorn, Swan, Penguin, Ape]
Remove items that are In Listb from Lista: [Unicorn, Koala, Hippo, Unicorn]