import java.util.HashMap;
import java.util.Map.Entry;
public class TestString {
public static void main(String[] args) {
System.gc();
String str = "deepak";
int length = str.length();
System.out.println("str " + str + " length " + length);
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0; i<length; i++){
char ch = str.charAt(i);
if(map.containsKey(ch)){
Integer val = map.get(ch);
map.put(ch, val+1);
}else{
map.put(ch, 1);
}
}
for (Entry<Character, Integer> entry : map.entrySet())
{
int hashCode = entry.hashCode();
char key = entry.getKey();
// int hash = hash();
System.out.println("hashcode " + hashCode + " hashcode of key>> " + entry.getKey().hashCode() + " key : " + key);
}
System.out.println(">>> " + map);
}
}
輸出:
STR迪帕克長度6爪哇 - 哈希映射retreival序列的密鑰哈希碼113哈希碼>> 112鍵:P
哈希碼96的密鑰哈希碼>> 97 key:a
hashcode密鑰的哈希碼>> 100 key:d
哈希碼103哈希碼密鑰>> 101鍵:電子
哈希碼106哈希碼的密鑰>> 107關鍵字:k
>>>
{p = 1時,A = 1,d = 1,E = 2,K = 1}
誰能幫我瞭解從程序和輸出兩件事情:通過地圖對象印刷
的數據,如何決定在內部序列? 例如。它是打印序列p,a,d,e,k。
entry.hashcode()和entry.key()。hashcode()有什麼區別? 請參考輸出解釋差異。
您的條目的內部順序不受HashMap的保證。他們可以在任何其他地方。看看[這裏](http://stackoverflow.com/questions/683518/java-class-that-implements-map-and-keeps-insertion-order)。 – GAlexMES
區別在[Java API](https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html#hashCode()) – GAlexMES
中有說明程序開始處的'System.gc()'? – Holger