maphashmap = new hashmap();
hashmap.put(new long(1), "a");
system.out.println(hashmap.get(new integer(1)));
發現取的值為null.
這是因為取值的時候沒有與key的型別匹配。
這是取值的邏輯**
final entrygetentry(object key)
return null;
}
但是integer的equals方法
public boolean equals(object obj)
return false;
}
long的equals方法
public boolean equals(object obj)
return false;
}
即使值相等,但因為型別不匹配,並且hashmap接受的引數型別是object,它這樣設計的用途是為了所有的型別都通用。但是hashmap本身就沒宣告說,一定要型別對才能取值。
哈哈以後要注意了。
C 使用變數時應注意其取值範圍
眾所周知,變數是有取值範圍的。像int double float char等等,都有其相應的取值範圍。平時在使用int型變數的時候,往往由於其用於小型的for迴圈,沒有超出取值範圍,因此忽視它的取值範圍。但是,了解其取值範圍是至關重要的。比如下面這段 輸入乙個int型變數並輸出它。假如輸入的數在in...
HashMap遍歷與按key排序。
google搜尋 hashmap 遍歷 寫道 第一種 map map new hashmap iterator iter map.entryset iterator while iter.hasnext 效率高,以後一定要使用此種方式!第二種 map map new hashmap iterator...
HashMap中通過key獲取value原始碼剖析
public v get object key private v getfornullkey for entrye table 0 e null e e.next return null 這段 我大致是這麼理解的,首先傳遞乙個key過來,判斷這個key是否存在,如果key為null,會走getfo...