集合刪除物件

2021-10-01 16:21:01 字數 1229 閱讀 6203

① list只依賴於equals方法

② set依賴於hashcode、equals方法

1、treeset 是二叉樹實現的,treeset中的資料是自動排好序的,不允許放入null值。 

2、hashset 是雜湊表實現的,hashset中的資料是無序的,可以放入null,但只能放入乙個null,兩者中的值都不能重複,就如資料庫中唯一約束。

3、hashset要求放入的物件必須實現hashcode()方法,放入的物件,是以hashcode碼作為標識的,而具有相同內容的 string物件,hashcode是一樣,所以放入的內容不能重複。

你重寫了物件的hashcode方法,並且內部用到了屬性來計算hashcode

那麼當你修改了屬性後,它的hashcode自然變了,那麼它就在原來的hashmap裡找不到了,自然刪除不掉。

思路:建立新set集合,篩選出刪除的物件

其他各種方法都沒用

setset1=aipropayrecordgather.getpropayset(); 

setset2=new treeset();

aipropaygather remo=new aipropaygather();

for(aipropaygather ai : set1)

}aipropayrecordgather.setpropayset(set2);

list呼叫remove(index)方法後,會移除index位置上的元素,index之後的元素就全部依次左移

for迴圈遍歷list刪除元素時,讓索引同步調整

for(int i=0;i倒序遍歷list刪除元素–正確!

for(int i=list.size()-1;i>=0;i--)

}system.out.println(list);

迭代刪除list元素–正確!

iteratorit=list.iterator();

while(it.hasnext())

}system.out.println(list);

注意: 迭代器這樣刪除是錯誤的

iteratorit=list.iterator();

while(it.hasnext())

}system.out.println(list);

Hibernate 刪除物件關聯集合遇到的問題

今天遇到乙個問題,product 類中有乙個集合屬性,我在修改product物件時,採用的是先將集合中的資料全部刪除,再重新儲存新的資料,但是在刪除的時候丟擲異常 刪除 如下 儲存產品與類別的關聯關係 setcategoryrels leaseproduct.gettleaseproductcate...

MongoDB 刪除集合

本章介紹如何在mongodb中刪除集合。drop 方法用於從資料庫中刪除集合。drop 命令的基本語法如下 db.collection name.drop 首先顯示資料庫mydb中的所有集合。use mydb switched to db mydb show collections mycollec...

MongoDB 刪除集合

本章節我們為大家介紹如何使用 mongodb 來刪除集合。mongodb 中使用 drop 方法來刪除集合。語法格式 db.collection.drop 引數說明 返回值如果成功刪除選定集合,則 drop 方法返回 true,否則返回 false。在資料庫 mydb 中,我們可以先通過 show ...