Eliminate list duplicate values
Java code
- public void Removeduplicate (List list) {
- HashSet h = new HashSet (list);
- List.clear ();
- List.addall (h);
- }
Intersection:
Java code
- List.retainall (Arrays.aslist (arr)); In the list is the intersection. ARR is an array
Subtraction
Java code
- List1.removeall (LIST2);
and set:
Java code
- List1.addall (LIST2);
- 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
- Package array;
- Import java.util.ArrayList;
- Import Java.util.HashSet;
- Import java.util.List;
- public class Listonlyvalue_1 {
- public static void Main (string[] args) {
- List<string> mylist = new arraylist<string> ();
- Mylist.add ("test");
- Mylist.add ("AB");
- Mylist.add ("CD");
- Mylist.add ("AB");
- Mylist.add ("SD");
- Mylist.add ("AB");
- Mylist.add ("CD");
- Mylist.add ("xyz");
- System.out.println ("List before processing:" + mylist);
- MyList = Removeduplicate (mylist);
- SYSTEM.OUT.PRINTLN ("Processed list:" + mylist);
- }
- public static list<string> removeduplicate (list<string> List) {
- hashset<string> HashSet = new hashset<string> (list);
- List.clear ();
- List.addall (HashSet);
- return list;
- }
- }
The result of the code operation is as follows:
- Pre-processing list:[test, AB, CD, AB, SD, AB, CD, XYZ]
- 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.
- Package array;
- Import java.util.ArrayList;
- Import Java.util.HashSet;
- Import Java.util.Iterator;
- Import java.util.List;
- public class Listonlyvalue_2 {
- public static void Main (string[] args) {
- List<string> mylist = new arraylist<string> ();
- Mylist.add ("test");
- Mylist.add ("AB");
- Mylist.add ("CD");
- Mylist.add ("AB");
- Mylist.add ("SD");
- Mylist.add ("AB");
- Mylist.add ("CD");
- Mylist.add ("xyz");
- System.out.println ("List before processing:" + mylist);
- MyList = Removeduplicatewithorder (mylist);
- SYSTEM.OUT.PRINTLN ("Processed list:" + mylist);
- }
- public static list<string> Removeduplicatewithorder (list<string> List) {
- hashset<string> HashSet = new hashset<string> ();
- list<string> NewList = new arraylist<string> ();
- for (Iterator Iterator = List.iterator (); Iterator.hasnext ();) {
- string element = (string) iterator.next ();
- if (Hashset.add (element)) {
- Newlist.add (Element);
- }
- }
- List.clear ();
- List.addall (NewList);
- return list;
- }
- }
The result of the code operation is as follows:
- Pre-processing list:[test, AB, CD, AB, SD, AB, CD, XYZ]
- 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