1、// 删除ArrayList中重复元素
1 public List removeDuplicate(List list) { 2 HashSet hashSet = new HashSet(list); 3 list.clear(); 4 list.addAll(hashSet); 5 return list; 6 }
2、// 删除ArrayList中重复元素,保持顺序
1 public static void removeDuplicateWithOrder(List list) { 2 Set set = new HashSet(); 3 List newList = new ArrayList(); 4 for (Iterator iter = list.iterator(); iter.hasNext();) { 5 Object element = iter.next(); 6 if (set.add(element)) 7 newList.add(element); 8 } 9 list.clear(); 10 list.addAll(newList); 11 System.out.println( " remove duplicate " + list); 12 }
3、// 删除ArrayList中重复元素
1 // 删除ArrayList中重复元素 2 public static void removeDuplicate(List list) { 3 for ( int i = 0 ; i < list.size() - 1 ; i ++ ) { 4 for ( int j = list.size() - 1 ; j > i; j -- ) { 5 if (list.get(j).equals(list.get(i))) { 6 list.remove(j); 7 } 8 } 9 } 10 System.out.println(list); 11 }
自己使用: 删除 “0.0”的值
List<List<String>> list1 = (List<List<String>>) map.get("商品入库表"); //表1 入库详细表
//删除list中 数量为 0值
1 for (Iterator<List<String>> item = list1.iterator(); item.hasNext(); ){ 2 List<String> it = item.next(); 3 System.out.print(it); 4 if (it.get(4).equals("0.0")) { 5 item.remove(); 6 } 7 }