Java finds the same and different elements of 2 sets (and removes duplicate elements from the list)

Source: Internet
Author: User
Tags addall

first, find the same and different elements in 2 sets

Here you need to use one of the methods provided by the collection collection: RemoveAll (cellection list)

The implementation code is as follows:

Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.List;
public class Test {
public static void Main (String args[]) {
Assemble a
List<string> _first=new arraylist<string> ();
_first.add ("Jim");
_first.add ("Tom");
_first.add ("Jack");
Set two
List<string> _second=new arraylist<string> ();
_second.add ("Jack");
_second.add ("Happy");
_second.add ("Sun");
_second.add ("good");

Collection exists=new arraylist<string> (_second);
Collection notexists=new arraylist<string> (_second);

Exists.removeall (_first);
System.out.println ("_second) not present in _set:" +exists);
Notexists.removeall (exists);
System.out.println ("_second" in _set: "+notexists);"
}
}

Results:

Elements in the _second that do not exist in _set: [Happy, Sun, good]
Elements in the _second that exist in _set: [Jack]


Second, remove the duplicate elements in the list (here is the simplest and most common method)

using attributes that are not duplicated by the hashset element (if the generic is an object, you need to implement the Equals and Hashcode methods)

@Test
public void Testotherlist () {
New List Collection
List nowlist=new ArrayList ();
add element
Nowlist.add (1);
Nowlist.add (2);
Nowlist.add (2);
Nowlist.add (55);
Nowlist.add (3);
Nowlist.add (1);
Nowlist.add (56);
Nowlist.add (56);
Using hashset elements to not repeat the feature
Nowlist=new ArrayList (New HashSet (nowlist));
System.out.println ("The collection after the removal of duplicate data:" +nowlist);
Result Printing: The collection after removing duplicate data: [1, 2, 3, 55, 57, 56]
}

third, the operation set, the intersection, the set and the difference set

writing class: Findnumber.java

Package day0527;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.Collection;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Set;

/**
*
* @ClassName: Findnumber
* @Description: a={6,3,9,3,2,4,5,7},b={5,8,6,2,1,9}, the output 3,4,7,1,8 idea: The complete elimination intersection, is the result
* @author
* @date May 27, 2016 9:56:25
*
*/
public class Findnumber {
public static void Main (string[] args) {
Note: Be sure to create an array using the format of the creation object
Integer[] A = new integer[] {6, 3, 9, 3, 2, 4, 5, 7};
Integer[] B = new integer[] {5, 8, 6, 2, 1, 9};
List _a = Arrays.aslist (a);
List _b = arrays.aslist (b);
Creating collections
Collection Reala = new arraylist<integer> (_a);
Collection realb = new arraylist<integer> (_b);
Find intersection
Reala.retainall (REALB);
SYSTEM.OUT.PRINTLN ("intersection result:" + reala);
Set result = new HashSet ();
Ask for the Complete works
Result.addall (_a);
Result.addall (_b);
System.out.println ("The Complete Result:" + results);
Finding the difference set: the result
Collection AA = new ArrayList (reala);
Collection BB = new ArrayList (result);
Bb.removeall (AA);
SYSTEM.OUT.PRINTLN ("Final result:" + BB);


/**
* Intersection Result: [6, 9, 2, 5] Complete: [1, 2, 3, 4, 5, 6, 7, 8, 9] End result: [1, 3, 4, 7, 8]
*/
}
}


Related Article

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.