2011-05-06 49 views
89

獲取與地圖中最大值關聯的關鍵點的最簡單方法是什麼?在Java地圖中與最大值關聯的查找鍵

我相信Collections.max(someMap)會返回最大密鑰,當你想要的密鑰對應的最大值。

+4

@Nishan:我看不出這是一個重複的。 – 2011-05-06 12:10:23

回答

93

基本上你需要迭代地圖的入口集,記住「當前已知的最大值」和與之關聯的關鍵字。 (或者只是入門含有的,當然。)

例如:

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; 
    } 
} 
+25

+1:您可以擁有多個具有相同最大值的密鑰。這個循環會給你找到它的第一個。 – 2011-05-06 12:19:07

+0

@Peter:好點:) – 2011-05-06 12:20:54

+17

將0改爲> = 0會給你找到的最後一個 – 2012-01-03 21:40:43

36

這段代碼將打印所有的按鍵與最大值

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 
      } 
     } 

    } 
} 
6

這裏是如何做直接做(沒有明確的額外循環)通過定義合適的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(); 
50

爲了完整起見,在這裏是做

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(); 
+1

'(entry1,entry2) - > entry1.getValue() - entry2 .getValue()'比較緊湊 – JustABit 2016-05-16 11:58:19

+0

謝謝@Nyx – Hilikus 2016-05-18 17:31:51

+3

如果我想要所有與最大值匹配的鍵,該怎麼辦? – Mouna 2016-06-13 11:45:13

0

我的項目的一個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; 
} 
1

此解決方案是否正常?

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); 
-1

您可以用做這樣的

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"); 
23

下一個簡單襯墊的Java-8

Key key = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey(); 
+1

不起作用,getKey()方法未找到 – Samir 2016-09-25 20:47:31

+3

@Samir https://docs.oracle.com/javase/ 7 /文檔/ API/JAVA /util/Map.Entry.html#getKey() – 2016-09-25 20:50:59

+2

最優雅和最小化的解決方案。謝謝 – 2017-06-04 20:14:20

4

返回一個可選的,因爲地圖上,如果可能沒有最大值的答案吧是空的: map.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey);

3

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()

2

我有兩種方法,使用這種方法來獲得與最大價值的關鍵並行化使用方法的最大值的條目:

Map.Entry<String, Integer> maxEntry = getMaxEntry(map); 

使用的Java 8我們可以得到含最大值的對象:

Object maxEntry = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();  

System.out.println("maxEntry = " + maxEntry);