2011-03-09 196 views
2

我想查找IntegerHashtable中的最大值。有沒有快速有效的方法來實現這一目標?JAVA哈希表查找最大值

這是我的代碼...

Hashtable<String,Integer> h = new Hashtable<String,Integer>(); 

h.add("a",1); 
h.add("b",5); 
h.add("c",3); 
h.add("d",5); 
h.add("e",2); 
h.add("f",1); 

int max = ???; 

我需要找到最大值,這在上面的例子中是5Hashtable將一直很小,平均少於100個條目。

+2

遍歷散列表? – iluxa 2011-03-09 00:23:56

+0

非常感謝球員 – 2011-03-09 00:42:47

回答

0

另一種方法:

new TreeSet(h.values()).last() 
+0

但它總是比迭代值慢。 – 2011-03-09 00:35:55

1

一)不寫

h.put ("a", 1); 

B)你能不能得到的值是這樣的:

java.util.Collection <Integer> ci = h.values(); 
// [1, 5, 3, 5, 2, 1] 

現在搜索值。