方法一:循环元素删除
// 删除ArrayList中重复元素public static void removeDuplicate1(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);}}}System.out.println(list);}java去除list重复值两种方式,一种无顺序,一种维持原list的元素顺序。Hastset根据hashcode判断是否重复,数据不会重复public static void removeDuplicate(ArrayList arlList) { HashSet h = new HashSet(arlList); arlList.clear(); arlList.addAll(h); } 通过Hashset的add方法判断是否已经添加过相同的数据,如果已存在相同的数据则不添加 public static void removeDuplicateWithOrder(ArrayList arlList) { Set set = new HashSet(); List newList = new ArrayList(); for (Iterator iter = arlList.iterator(); iter.hasNext(); ) { Object element = iter.next(); if (set.add(element)) newList.add(element); } arlList.clear(); arlList.addAll(newList); }