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 > 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) { 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 voidremoveduplicatewithorder (List list) {Set Set=NewHashSet (); List NewList=NewArrayList (); 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) { new ArrayList (); for (int i=0;i<list.size (); i++) { if(! Listtemp.contains (List.get (i))) { Listtemp.add (List.get (i)); } } return listtemp; }
Reprint: http://blog.csdn.net/u011728105/article/details/46594963
A method of removing duplicate data from a list collection in Java