[Conversion] Java list set to remove duplicate data
1. Loop all 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. Use hashset to remove duplicate elements
public static List removeDuplicate(List list) { HashSet h = new HashSet(list); list.clear(); list.addAll(h); return list; }
3. Delete repeated elements in the arraylist in order
// Delete repeated elements in the arraylist in the order of public static void removeduplicatewithorder (list) {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. traverse the objects in the list and use list. contain (). If it does not exist, store the objects in another list set.
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; }
Reprinted: http://blog.csdn.net/u011728105/article/details/46594963
List set to remove duplicate data