从hashmap获取具有最大值的键?

我有一个像这样定义的HashMap ……

 HashMap uniqueNames = new HashMap(); 

它存储名称和该名称的出现。 例如…

 uniqueNames.put("lastname",42); 

如何获得出现次数最多的名称?

有关更多信息,我正在使用“people”的二叉搜索树,将唯一的名称和频率存储在HashMap 。 我想要做的是打印最常见的姓氏,有人告诉我使用HashMap因为我想将StringInteger一起存储。 也许我应该使用一个类来存储名称和频率? 请有人请提供一些建议。

如果你必须使用HashMap,那么最简单的方法可能就是遍历Map寻找最大值

 Entry maxEntry = null; for(Entry entry : uniqueNames.entrySet()) { if (maxEntry == null || entry.getValue() > maxEntry.getValue()) { maxEntry = entry; } } // maxEntry should now contain the maximum, 

最明显的是,现在允许具有最大出现值的多个:

 Integer largestVal = null; List> largestList = new ArrayList>(); for (Entry i : uniqueNames.entrySet()){ if (largestVal == null || largestVal < i.getValue()){ largestVal = i.getValue(); largestList .clear(); largestList .add(i); }else if (largestVal == i.getValue()){ largestList .add(i); } } 

另一种选择是使用Guava的BiMap。

 BiMap uniqueNames = ...; List values = Lists.newArrayList(uniqueNames.values()); Collections.sort(values); String name = uniqueNames.inverse().get(values.get(0)); 

实际上有两种方法可以解决这个问题。

如果您要经常这样做,我实际上建议反向存储映射,其中键是名称出现的次数,值是多次出现的名称列表。 我也会使用HashMap在另一个方向上执行查找。

 TreeMap > sortedOccurrenceMap = new TreeMap > (); HashMap  lastNames = new HashMap  (); boolean insertIntoMap(String key) { if (lastNames.containsKey(key)) { int count = lastNames.get(key); lastNames.put(key, count + 1); //definitely in the other map ArrayList  names = sortedOccurrenceMap.get(count); names.remove(key); if(!sortedOccurrenceMap.contains(count+1)) sortedOccurrenceMap.put(count+1, new ArrayList()); sortedOccurrenceMap.get(count+1).add(key); } else { lastNames.put(key, 1); if(!sortedOccurrenceMap.contains(1)) sortedOccurrenceMap.put(1, new ArrayList()); sortedOccurrenceMap.get(1).add(key); } } 

删除类似的东西……

最后,为了您的搜索:

 ArrayList  maxOccurrences() { return sortedOccurrenceMap.pollLastEntry().getValue(); } 

返回具有最大出现次数的名称列表。

如果你这样做,搜索可以在O(log n)中完成,但空间需求会增加(尽管只是一个常数因子)。

如果空间是一个问题,或性能不是问题,只需遍历uniqueNames.keySet并跟踪最大值。

看起来你想要的东西有点像SortedMap但是一个按照值排序,而不是键。 我不认为标准API中存在这样的东西。

最好创建一个Frequency类并将实例存储在SortedSet

 import java.util.Set; import java.util.TreeSet; public class Frequency implements Comparable { private String name; private int freq; public Frequency(String name, int freq) { this.name = name; this.freq = freq; } public static void main(String[] args) { Set set = new TreeSet(); set.add(new Frequency("fred", 1)); set.add(new Frequency("bob", 5)); set.add(new Frequency("jim", 10)); set.add(new Frequency("bert", 4)); set.add(new Frequency("art", 3)); set.add(new Frequency("homer", 5)); for (Frequency f : set) { System.out.println(f); } } @Override public boolean equals(Object o) { if (o == null) return false; if (o.getClass().isAssignableFrom(Frequency.class)) { Frequency other = (Frequency)o; return other.freq == this.freq && other.name.equals(this.name); } else { return false; } } @Override public int compareTo(Frequency other) { if (freq == other.freq) { return name.compareTo(other.name); } else { return freq - other.freq; } } @Override public String toString() { return name + ":" + freq; } } 

输出:

弗雷德:1
艺术:3
BERT:4
鲍勃:5
本垒打:5
吉姆:10

 List list= new ArrayList(); HashMap map=new HashMap(); for(String string: list) { if(map.containsKey(string)) { map.put(string, map.get(string)+1); } else { map.put(string, 1); } } Entry maxEntry = null; for(Entry entry : map.entrySet()) { if (maxEntry == null || entry.getValue() > maxEntry.getValue()) { maxEntry = entry; } } 

如果你只想要价值,你可以为此而努力。 在这个例子中,我必须得到’n’个数组中一个数字的最大频率

  { int n = sc.nextInt(); int arr[] = new int[n]; int freq = 1; int i; Map myMap = new HashMap(); for(i=0;imax) max = myMap.get(arr[i]); } System.out.println(max); } 

这是我的方法。

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class FindWordCounter { public static void main(String[] args) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Enter the sentence: "); String sentence = bufferedReader.readLine(); FindWordCounter.countWord(sentence); } catch (IOException e) { System.out.println(e); } } public static void countWord(String sentence) { Map hashMap = new HashMap(); String[] word = sentence.toLowerCase().split(" "); for (int i=0; i maxCount = null; for(Entry entry : hashMap.entrySet()) { if (maxCount == null || entry.getValue() > maxCount.getValue()) { maxCount = entry; } } System.out.println("The word with maximum occurence is: " + maxCount.getKey() + " and the number of occurence is: " + maxCount.getValue()); } }