打印 HashMap 中第 N 个最高值的键
我有一个hashmap
,必须在hashmap
中打印n
- 最高值。
我设法获得了最高的 value 。
我首先对哈希图进行了排序,以便如果有 两个键 相同的值 ,那么我得到了第一个键的钥匙字母顺序 。
但是我仍然不知道如何获得最高价值的钥匙?
public void(HashMap map, int n) {
Map<String, Integer> sortedmap = new TreeMap<>(map);
Map.Entry<String, Integer> maxEntry = null;
for (Map.Entry<String, Integer> entry : sortedmap.entrySet()) {
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
maxEntry = entry;
}
}
System.out.println(maxEntry.getKey());
}
I have a HashMap
and have to print the N
-th highest value in the HashMap
.
I have managed to get the highest value.
I have sorted the HashMap first so that if there are two keys with the same value, then I get the key that comes first alphabetically.
But I still don't know how to get the key for nth highest value?
public void(HashMap map, int n) {
Map<String, Integer> sortedmap = new TreeMap<>(map);
Map.Entry<String, Integer> maxEntry = null;
for (Map.Entry<String, Integer> entry : sortedmap.entrySet()) {
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
maxEntry = entry;
}
}
System.out.println(maxEntry.getKey());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种方法。 n最高>必须忽略重复项。否则,您将询问地图中的位置,而不是与其他人相比的内在值。例如,如果这些值为
8,8,8,7,7,5,5,3,2,1
,则3rd
最高值是5
其中值8
将仅在3rd
降序排序列表的位置中值。max
tointeger.max_value
。treemap
已经通过键进行排序,并且是stable Sort
(请参阅分类算法)钥匙将保持序列的顺序值。小于
,这就是忽略通过列表迭代时的重复项。max
和减少n
。还要分配键印刷
Here is one way. It is presumed by Nth highest that duplicates must be ignored. Otherwise you would be asking about position in the map and not the intrinsic value as compared to others. For example, if the values are
8,8,8,7,7,5,5,3,2,1
then the3rd
highest value is5
where the value8
would be simply be value in the3rd
location of a descending sorted list.found
tofalse
andmax
toInteger.MAX_VALUE
.TreeMap
is already sorted by keys and is astable sort
(see Sorting algorithms) the keys will remain in sorted order for duplicate values.less than
, That is what ignores the duplicates when iterating thru the list.max
and decrementn
. Also assign the keyfound
totrue
and break out of the loop.nth
largest exists.prints
此问题不需要对所有给定数据进行排序。如果
n
接近1
,则会造成巨大的开销,在这种情况下,可能的解决方案将以线性时间O(n)运行。排序将时间复杂度增加到O(n*log n)(如果您不熟悉大 O 表示法,您可能有兴趣阅读此问题的答案)。对于任何小于地图大小的n
,部分排序将是更好的选择。如果我理解正确,则需要考虑重复的值。例如,对于
n=3
值12,12,10,8,5
,第三大值将为10
(如果您不重复,则可以简化以下解决方案)。我建议通过以下步骤解决此问题:
PriorityQueue
作为n
最高值的容器。PriorityQueue
基于所谓的最小堆数据结构。在实例化PriorityQueue
时,您需要提供一个Comparator
或者队列的元素必须具有自然排序顺序,即实现接口Comparable
(其中Integer
就是这种情况)。element()
和peek()
方法将从优先级队列中检索最小元素。并且队列将包含给定映射中的n
个最大值,其最小元素将是映射的第n
个最高值。实现可能如下所示:
注意,如果您不熟悉 Java 8 方法
merge()
,在getReversedMap()
中您可以替换如下:main()
- demoOutput (集合中的第三大值
12, 12, 10, 10 , 9, 5, 3, 3
)This problem doesn't require sorting all the given data. It will cause a huge overhead if
n
is close to1
, in which case the possible solution will run in a linear time O(n). Sorting increases time complexity to O(n*log n) (if you are not familiar with Big O notation, you might be interested in reading answers to this question). And for anyn
less than map size, partial sorting will be a better option.If I understood you correctly, duplicated values need to be taken into account. For instance, for
n=3
values12,12,10,8,5
the third-largest value will be10
(if you don't duplicate then the following solution can be simplified).I suggest approaching this problem in the following steps:
PriorityQueue
as container forn
highest values.PriorityQueue
is based on the so called min heap data structure. While instantiatingPriorityQueue
you either need to provide aComparator
or elements of the queue has to have a natural sorting order, i.e. implement interfaceComparable
(which is the case forInteger
). Methodselement()
andpeek()
will retrieve the smallest element from the priority queue. And the queue will containn
largest values from the given map, its smallest element will be then
-th highest value of the map.The implementation might look like this:
Note, if you are not familiar with Java 8 method
merge()
, insidegetReversedMap()
you can replace it this with:main()
- demoOutput (the third-greatest value from the set
12, 12, 10, 10, 9, 5, 3, 3
)当找到第 k 个最高值时,您应该考虑使用优先级队列(也称为堆)或使用快速选择。
堆可以在 O(n) 时间内构建,但是如果初始化它并插入 n 个元素,则将花费 O(nlogn) 时间。之后,您可以弹出 k 个元素以获得第 k 个最高元素
快速选择是一种算法,旨在在 O(n) 时间内查找第 n 个最高元素
When finding the kth highest value, you should consider using a priority queue (aka a heap) or using quick select.
A heap can be constructed in O(n) time however if you initialize it and insert n elements, it will take O(nlogn) time. After which you can pop k elements in order to get the kth highest element
Quick select is an algorithm designed for finding the nth highest element in O(n) time