|
原帖由 greenflute 于 2008-2-16 23:36 发表 ![]()
FYI:
Please note the time when the hashCode() method of k1 and k2 were called.
o. create k1, k2, which k1.i=1, k2.i=2
o. add k1 to set, k1.hashCode() will be called, so the set find a proper place according to k1's hashCode, a.k.a. "1".
o. add k2 to set, k2.hashCode() will be called, it is "2", so the set find another place for k2.
o. change k2.i=1, watch out here, the "place" where k2 reside in the set will NOT change! there is NO dynamic duplicate check nor event notification in set.
o. delete k1, k1.hashCode() will be called once again. because the nature of set, the set will NOT search through all the background hashmap to delete all object which has "1" as hashcode. so there will be ONLY one object being deleted. k2 will NOT be deleted althought it DOES has a hashcode=1, but it's place in the set is according to "2".
o. now there is only k2 in the set, which's Key place in the set is "2", although in the mean time it has a hashcode "1".
o. trying to delete k2, this time k2.hashCode() will be called again, but there is not such object in the set with "1" as key, so deletion FAILED.
o. change k2.i=2
o. try to delete k2 again, this time we got a "2" from k2.hashCode(), so we can directly find the k2 object in the set, because there IS a key ojbect in the coressponding place to "2". so deletion SUCCESSED.
明了了,很牛; |
|