Java elimination list duplicate values and intersection, set, difference set

Source: Internet
Author: User

Eliminate list duplicate values

Java code
    1. public void Removeduplicate (List list) {
    2. HashSet h = new HashSet (list);
    3. List.clear ();
    4. List.addall (h);
    5. }



Intersection:

Java code
    1. List.retainall (Arrays.aslist (arr)); In the list is the intersection. ARR is an array




Subtraction

Java code
    1. List1.removeall (LIST2);



and set:

Java code
    1. List1.addall (LIST2);
    2. Removeduplicate (List1);//clear duplicate values as previously mentioned

Java handles duplicate values in a list in two ways, one is to get a new unordered list after processing, and the other to keep the list in its original order.

Method One: Use Hastset's Hashcode method to determine if duplicates are repeated, the data is not duplicated, but the order is changed
  1. Package array;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashSet;
  4. Import java.util.List;
  5. public class Listonlyvalue_1 {
  6. public static void Main (string[] args) {
  7. List<string> mylist = new arraylist<string> ();
  8. Mylist.add ("test");
  9. Mylist.add ("AB");
  10. Mylist.add ("CD");
  11. Mylist.add ("AB");
  12. Mylist.add ("SD");
  13. Mylist.add ("AB");
  14. Mylist.add ("CD");
  15. Mylist.add ("xyz");
  16. System.out.println ("List before processing:" + mylist);
  17. MyList = Removeduplicate (mylist);
  18. SYSTEM.OUT.PRINTLN ("Processed list:" + mylist);
  19. }
  20. public static list<string> removeduplicate (list<string> List) {
  21. hashset<string> HashSet = new hashset<string> (list);
  22. List.clear ();
  23. List.addall (HashSet);
  24. return list;
  25. }
  26. }

The result of the code operation is as follows:

    1. Pre-processing list:[test, AB, CD, AB, SD, AB, CD, XYZ]
    2. Processed list:[test, SD, AB, XYZ, CD]
Method Two: Through the HashSet Add method to determine whether the same data has been added, if there is already the same data is not added, maintain the order of the elements in the list.
  1. Package array;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashSet;
  4. Import Java.util.Iterator;
  5. Import java.util.List;
  6. public class Listonlyvalue_2 {
  7. public static void Main (string[] args) {
  8. List<string> mylist = new arraylist<string> ();
  9. Mylist.add ("test");
  10. Mylist.add ("AB");
  11. Mylist.add ("CD");
  12. Mylist.add ("AB");
  13. Mylist.add ("SD");
  14. Mylist.add ("AB");
  15. Mylist.add ("CD");
  16. Mylist.add ("xyz");
  17. System.out.println ("List before processing:" + mylist);
  18. MyList = Removeduplicatewithorder (mylist);
  19. SYSTEM.OUT.PRINTLN ("Processed list:" + mylist);
  20. }
  21. public static list<string> Removeduplicatewithorder (list<string> List) {
  22. hashset<string> HashSet = new hashset<string> ();
  23. list<string> NewList = new arraylist<string> ();
  24. for (Iterator Iterator = List.iterator (); Iterator.hasnext ();) {
  25. string element = (string) iterator.next ();
  26. if (Hashset.add (element)) {
  27. Newlist.add (Element);
  28. }
  29. }
  30. List.clear ();
  31. List.addall (NewList);
  32. return list;
  33. }
  34. }

The result of the code operation is as follows:

    1. Pre-processing list:[test, AB, CD, AB, SD, AB, CD, XYZ]
    2. Processed List:[test, AB, CD, SD, XYZ]

This method uses the HashSet add property, if an element already exists in HashSet, the Add method will not add this element to the HashSet, it is the use of this principle, when the hashset can add an element, NewList also follow add an element, This gives you a new list that maintains the original order without duplicate values.

Java elimination list duplicate values and intersection, set, difference set

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.