1. Loop through all the elements in the list and delete duplicates
Public static list removeduplicate (list list) { for (int i = 0; I < list.size () - 1; i + +) {for (int J = list.size () - 1; j< C22/>> i; J--) { if (List.get (j). Equals (List.get (i))) { list.remove (j); } } } return list; }
2. Remove duplicate elements by HashSet
public static list removeduplicate (List list) { HashSet h = new HashSet (list); List.clear (); List.addall (h); return list; }
3. Delete duplicate elements in ArrayList, keep order
Delete duplicate elements in ArrayList, keep order public static void Removeduplicatewithorder (List list) { Set set = new HashSet (); List NewList = new ArrayList (); for (Iterator iter = List.iterator (); Iter.hasnext ();) { Object element = Iter.next (); if (Set.add (Element)) Newlist.add (element); } List.clear (); List.addall (newlist); System.out.println ("Remove duplicate" + list); }
4. Iterate through the objects in the list, use List.contain (), and put them in another list if they don't exist.
public static list removeduplicate (List list) { list listtemp = new ArrayList (); for (int i=0;i<list.size (); i++) { if (!listtemp.contains (List.get (i))) { Listtemp.add (List.get (i)); } } return listtemp; }
set<string> s = new hashset<string> ();
for (int i=0;i<list.size (); i++) {
Boolean B = S.add (List.get (i));
if (!b) {
SYSTEM.OUT.PRINTLN ("Duplicate value ===================================:" +list.get (i));
}
}
A method of removing duplicate data from a list collection in Java