During the development process, we may need to process data from 2 or more list collections, such as multiple list collection data for the same element, and multiple list collection data to get only the data that belongs to itself, as shown:
Write a picture description here
Here is a list of 2 lists, some of which require us to handle 3 of the situation on the graph.
* Only belongs to A
* Common Elements
* Only belongs to B
This kind of processing is not unfamiliar in mathematics, only belongs to a, equivalent to a set of B on a relative complement set, the same elements, A and B of the sum, only B, equivalent to the set of a on the relative complement of B. Having understood these concepts, let's look at how Java code is implemented and the code below
public static void Test () { list<integer> A = new arraylist<integer> (); A.add (1); A.add (2); A.add (3); A.add (4); list<integer> B = new arraylist<integer> (); B.add (2); B.add (4); B.add (5); B.add (6); Collection C = new arraylist<integer> (A); C.retainall (B); System.out.println ("A and B-set:" + C); B.removeall (C); System.out.println ("A relative complement to B:" + b); A.removeall (C); System.out.println ("B relative complement set on a:" + a); }
Results from running:
This article quoted: 71214126
Java 2 List collection data seek and complement operations