Java 2 List collection data seek and complement operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.