回答
基本上你需要迭代地圖的入口集,記住「當前已知的最大值」和與之關聯的關鍵字。 (或者只是入門含有的,當然。)
例如:
Map.Entry<Foo, Bar> maxEntry = null;
for (Map.Entry<Foo, Bar> entry : map.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
+1:您可以擁有多個具有相同最大值的密鑰。這個循環會給你找到它的第一個。 – 2011-05-06 12:19:07
@Peter:好點:) – 2011-05-06 12:20:54
將0改爲> = 0會給你找到的最後一個 – 2012-01-03 21:40:43
這段代碼將打印所有的按鍵與最大值
public class NewClass4 {
public static void main(String[] args)
{
HashMap<Integer,Integer>map=new HashMap<Integer, Integer>();
map.put(1, 50);
map.put(2, 60);
map.put(3, 30);
map.put(4, 60);
map.put(5, 60);
int maxValueInMap=(Collections.max(map.values())); // This will return max value in the Hashmap
for (Entry<Integer, Integer> entry : map.entrySet()) { // Itrate through hashmap
if (entry.getValue()==maxValueInMap) {
System.out.println(entry.getKey()); // Print the key with max value
}
}
}
}
這裏是如何做直接做(沒有明確的額外循環)通過定義合適的Comparator
:
int keyOfMaxValue = Collections.max(
yourMap.entrySet(),
new Comparator<Entry<Double,Integer>>(){
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
return o1.getValue() > o2.getValue()? 1:-1;
}
}).getKey();
爲了完整起見,在這裏是做
countMap.entrySet().stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().getKey();
或
Collections.max(countMap.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue()).getKey();
或
Collections.max(countMap.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
我的項目的一個Java 8路,我用Jon的和Fathah的略加修改的版本解。在具有相同值的多個條目的情況下,它返回它找到的最後一個條目:
public static Entry<String, Integer> getMaxEntry(Map<String, Integer> map) {
Entry<String, Integer> maxEntry = null;
Integer max = Collections.max(map.values());
for(Entry<String, Integer> entry : map.entrySet()) {
Integer value = entry.getValue();
if(null != value && max == value) {
maxEntry = entry;
}
}
return maxEntry;
}
此解決方案是否正常?
int[] a = { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7 };
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i : a) {
Integer count = map.get(i);
map.put(i, count != null ? count + 1 : 0);
}
Integer max = Collections.max(map.keySet());
System.out.println(max);
System.out.println(map);
您可以用做這樣的
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
hm.put(1,10);
hm.put(2,45);
hm.put(3,100);
Iterator<Integer> it = hm.keySet().iterator();
Integer fk = it.next();
Integer max = hm.get(fk);
while(it.hasNext()) {
Integer k = it.next();
Integer val = hm.get(k);
if (val > max){
max = val;
fk=k;
}
}
System.out.println("Max Value "+max+" is associated with "+fk+" key");
下一個簡單襯墊的Java-8
Key key = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();
不起作用,getKey()方法未找到 – Samir 2016-09-25 20:47:31
@Samir https://docs.oracle.com/javase/ 7 /文檔/ API/JAVA /util/Map.Entry.html#getKey() – 2016-09-25 20:50:59
最優雅和最小化的解決方案。謝謝 – 2017-06-04 20:14:20
返回一個可選的,因爲地圖上,如果可能沒有最大值的答案吧是空的: map.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey);
Java 8獲取所有密鑰的方法最大值。
public static Entry<String, Integer> getMaxEntry(Map<String, Integer> map){
Entry<String, Integer> maxEntry = null;
Integer max = Collections.max(map.values());
for(Entry<String, Integer> entry : map.entrySet()) {
Integer value = entry.getValue();
if(null != value && max == value) {
maxEntry = entry;
}
}
return maxEntry;
}
舉個例子剛開:
Integer max = PROVIDED_MAP.entrySet()
.stream()
.max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1)
.get()
.getValue();
List listOfMax = PROVIDED_MAP.entrySet()
.stream()
.filter(entry -> entry.getValue() == max)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
System.out.println(listOfMax);
您也可以通過使用parallelStream()
代替stream()
我有兩種方法,使用這種方法來獲得與最大價值的關鍵並行化使用方法的最大值的條目:
Map.Entry<String, Integer> maxEntry = getMaxEntry(map);
使用的Java 8我們可以得到含最大值的對象:
Object maxEntry = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();
System.out.println("maxEntry = " + maxEntry);
- 1. 如何查找與HashMap中最大值關聯的鍵?
- 2. 查找關聯數組的最小和最大鍵和值php
- 3. 查找與字典中的值關聯的最大數字
- 4. C++地圖查找值和關聯的鍵
- 5. 查找鍵值在地圖
- 6. 在javascript數組中查找最大值的關鍵字
- 7. 獲取與Map中相應最大值關聯的鍵(TreeMap/HashMap)
- 8. 在數據結構中查找最大值最大值和最大鍵值
- 9. 查找本地最大值
- 10. 查找最大值 - Java
- 11. 查找最小值和最大值JAVA
- 12. 在地圖中查找鍵值
- 13. Java:查找數組中的最大值
- 14. 在Java中查找num值和最小值/最大值值
- 15. 如何在Rascal中查找地圖的最大值?
- 16. 在地圖中查找最小值
- 17. 在地塊上查找最大y值
- 18. 找到與關聯最大值正確的方法軌
- 19. 找到與python文件中的關鍵字相關聯的值
- 20. 查找地圖中的關鍵,給定的值
- 21. 如何在C++中查找最大值地圖
- 22. 從另一列中查找與某個特定ID相關聯的一列中的最小值和最大值
- 23. 查找地圖中最高的n值
- 24. 查找與關鍵最小值數組中多維數組
- 25. C++幫助查找地圖中的最大值
- 26. 在Java中查找最小,最大和中間值
- 27. 如何在所有文檔中查詢與鍵關聯的值?
- 28. 查找最大與R中
- 29. 函數查找本地最大值
- 30. 地圖查找匿名結構關鍵
@Nishan:我看不出這是一個重複的。 – 2011-05-06 12:10:23