一:
Hastset根據hashcode判斷是否重復,數據不會重復
Java代碼
- /** List order not maintained **/
- public static void removeDuplicate(ArrayList arlList)
- {
- HashSet h = new HashSet(arlList);
- arlList.clear();
- arlList.addAll(h);
- }
二:
通過Hashset的add方法判斷是否已經添加過相同的數據,如果已存在相同的數據則不添加
Java代碼
- /** List order maintained **/
- 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);
- }
以下來自網絡:
方法一:循環元素刪除
// 刪除ArrayList中重復元素
public static void 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);
}
}
}
System.out.println(list);
}
方法二:通過HashSet剔除
// 刪除ArrayList中重復元素
public static void removeDuplicate(List list)
{
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
System.out.println(list);
}
方法三: 刪除ArrayList中重復元素,保持順序
// 刪除ArrayList中重復元素,保持順序
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);
}
自己使用: 刪除 “0.0”的值
List<List<String>> list1 = (List<List<String>>) map.get("商品入庫表"); //表1 入庫詳細表
//刪除list中 數量為 0值
for (Iterator<List<String>> item = list1.iterator(); item.hasNext(); ) {
List<String> it = item.next();
System.out.print(it);
if (it.get(4).equals("0.0")) {
item.remove();
}
}
鏈接地址:http://iteye.blog.163.com/blog/static/186308096201302565345510/
