Java 如何从hashmap中获得5个最高值?
我有一个Hashmap,它将zipcodes存储为键,并将填充作为值存储在Hashmap中 hashmap包含大约33k个条目 我试图从5个邮政编码中获得5个最高的人口值,并打印出与5个最高人口相关联的5个邮政编码,但我在理解算法方面遇到了困难 如果只是一个,很容易,但是5个限制给我带来了一些麻烦 我知道将5个值存储在int数组中,我有一个计数器来确定何时存储其中5个值,但仅此而已 谢谢Java 如何从hashmap中获得5个最高值?,java,Java,我有一个Hashmap,它将zipcodes存储为键,并将填充作为值存储在Hashmap中 hashmap包含大约33k个条目 我试图从5个邮政编码中获得5个最高的人口值,并打印出与5个最高人口相关联的5个邮政编码,但我在理解算法方面遇到了困难 如果只是一个,很容易,但是5个限制给我带来了一些麻烦 我知道将5个值存储在int数组中,我有一个计数器来确定何时存储其中5个值,但仅此而已 谢谢 int populatedCounter = 0; int[] populatedZip
int populatedCounter = 0;
int[] populatedZip = new int[5];
it = zipCodePop.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
for (int i = 0; i < populatedZip.length; i++)
{
}
}
}
int-populatedCounter=0;
int[]populatedZip=新int[5];
it=zipCodePop.entrySet().iterator();
while(it.hasNext())
{
Map.Entry pairs=(Map.Entry)it.next();
for(int i=0;i
没有电脑,只用一张纸和一支铅笔,你会怎么做?假设你有一堆上面有数字的索引卡,你的工作就是找到5个最高的数字。你会怎么做?写下其他人可以遵循的实现目标的步骤,当你写下这些步骤时,你就会有一个算法,你可以开始考虑用代码实现
你说一个最大值很容易,所以你要像一个最大值一样去做,但是要记住五个最大值。在这里,一组最大值可能会有所帮助。使用标准方法,并假设总体计数在
HashMap
中存储为Integer
s,尝试此操作:
List<Integer> list = new ArrayList<Integer>(zipCodePop.values());
Collections.sort(list, Collections.reverseOrder());
List<Integer> top5 = list.subList(0, 5);
List List=newarraylist(zipCodePop.values());
Collections.sort(list,Collections.reverseOrder());
列表top5=列表。子列表(0,5);
也会有帮助,而且是一个关于如何从列表中获得前k的好话题,您可以查看
PriorityQueue p=新的PriorityQueue(5);
int[]a=新的int[]{3,5,10,1,23,42,661333545110};
对于(int i:a){
p、 加(i);
如果(p.size()>5){
p、 poll();
}
}
//产量将达到最高值5,[42,66,110,1333,545]
您可以使用O(n log(k))时间复杂度//k是您的最高值计数。将此类集合的条目放入列表并对其排序是一个选项。但33k元素是一个数字,排序的O(n*log(n))复杂性可能已经对性能产生了显著影响 一种方法是使用nr4bt已经提到的优先队列(我在他回答时写了这个片段)。它基本上是将所有元素插入到根据映射项的值排序的PriorityQueue中
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
public class GreatestOfMap
{
public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zip000", 1234);
map.put("zip001", 2345);
map.put("zip002", 3456);
map.put("zip003", 4567);
map.put("zip004", 5678);
map.put("zip005", 6789);
map.put("zip006", 123);
map.put("zip007", 234);
map.put("zip008", 456);
map.put("zip009", 567);
map.put("zip010", 7890);
map.put("zip011", 678);
map.put("zip012", 789);
map.put("zip013", 890);
int n = 5;
List<Entry<String, Integer>> greatest = findGreatest(map, 5);
System.out.println("Top "+n+" entries:");
for (Entry<String, Integer> entry : greatest)
{
System.out.println(entry);
}
}
private static <K, V extends Comparable<? super V>> List<Entry<K, V>>
findGreatest(Map<K, V> map, int n)
{
Comparator<? super Entry<K, V>> comparator =
new Comparator<Entry<K, V>>()
{
@Override
public int compare(Entry<K, V> e0, Entry<K, V> e1)
{
V v0 = e0.getValue();
V v1 = e1.getValue();
return v0.compareTo(v1);
}
};
PriorityQueue<Entry<K, V>> highest =
new PriorityQueue<Entry<K,V>>(n, comparator);
for (Entry<K, V> entry : map.entrySet())
{
highest.offer(entry);
while (highest.size() > n)
{
highest.poll();
}
}
List<Entry<K, V>> result = new ArrayList<Map.Entry<K,V>>();
while (highest.size() > 0)
{
result.add(highest.poll());
}
return result;
}
}
import java.util.ArrayList;
导入java.util.Comparator;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
导入java.util.PriorityQueue;
公共类GreatestOfMap
{
公共静态void main(字符串[]args)
{
Map Map=newhashmap();
地图放置(“zip000”,1234);
地图放置(“zip001”,2345);
地图放置(“zip002”,3456);
地图放置(“zip003”,4567);
地图放置(“zip004”,5678);
地图放置(“zip005”,6789);
地图放置(“zip006”,123);
地图放置(“zip007”,234);
地图放置(“zip008”,456);
地图放置(“zip009”,567);
地图放置(“zip010”,7890);
地图放置(“zip011”,678);
地图放置(“zip012”,789);
地图放置(“zip013”,890);
int n=5;
列表最大值=FindCreatest(图5);
System.out.println(“Top”+n+”条目:);
对于(条目:最大)
{
系统输出打印项次(输入);
}
}
私有静态公共类CheckHighiestValue{
公共静态无效主(字符串…s){
HashMap map=newhashmap();
地图放置(“第一”,10000);
地图放置(“第二”,20000);
地图放置(“第三”,300);
地图放置(“第四”,800012);
地图放置(“第五”,5000);
普特地图(“第六”,30012);
普特地图(“第七”,1234);
普特地图(“第八”,45321);
地图.put("九",5678);;
Set=map.entrySet();
列表=新的ArrayList(
设置);
Collections.sort(list,newcomparator(){
@凌驾
公共整数比较(条目o1,
入口(氧气){
返回o2.getValue().compareTo(o1.getValue());
}
});
System.out.println(list.subList(0,5));
}
}这是我制作的东西,希望能为您提供一些您想要使用的东西
public class TopsCollection {
private static Map<String, Integer> collectors = new HashMap<>();
public TopsCollection() {
}
public void add(String playerName, int score) {
collectors.put(playerName, score);
}
public void clearCollectors() {
synchronized (collectors) {
collectors.clear();
}
}
public List<Map.Entry<String, Integer>> getTops() {
return collectors.entrySet().stream().sorted(comparing(Map.Entry::getValue, reverseOrder())).limit(5).collect(toList());
}
public int getTopByName(String name) {
for (int i = 0; i < getTops().size(); i++) {
if (getTops().get(i).getKey().contains(name)) {
return i;
}
}
return 0;
}
公共类集合{
私有静态映射收集器=new HashMap();
公共收藏(){
}
公共无效添加(字符串播放器名称,整数分数){
收集者。放置(playerName,score);
}
公共图书馆{
已同步(收集器){
collectors.clear();
}
}
公共列表getTops(){
返回collectors.entrySet().stream().sorted(比较(Map.Entry::getValue,reverseOrder()).limit(5.collect(toList());
}
public int getTopByName(字符串名称){
对于(int i=0;i
getTopByName允许您获得指定名称的顶部位置。是像Guava fair game这样的第三方库吗?这可能只是一行排序.natural().greatestOf(map.values(),5)
。创建一个最大值堆如何;删除最大值,然后调整大小;获取下一个最大值,依此类推。ZIP和填充是什么(映射的参数是什么)?Map
?这是家庭作业吗?顺便说一句,看看哪一个可以泛化以找到k个最大的数字。算法:向数组中添加前5个值,对数组进行排序,遍历映射,直到找到比数组中第一个值(最小值)高的值,替换最小值,使用数组(如果新值高于其他值),请继续。保留键/值对需要更多的努力,但应该是这样
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("first", 10000);
map.put("second", 20000);
map.put("third", 300);
map.put("fourth", 800012);
map.put("fifth", 5000);
map.put("sixth", 30012);
map.put("seventh", 1234);
map.put("eighth", 45321);
map.put("nineth", 5678);
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
set);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
System.out.println(list.subList(0, 5));
}
public class TopsCollection {
private static Map<String, Integer> collectors = new HashMap<>();
public TopsCollection() {
}
public void add(String playerName, int score) {
collectors.put(playerName, score);
}
public void clearCollectors() {
synchronized (collectors) {
collectors.clear();
}
}
public List<Map.Entry<String, Integer>> getTops() {
return collectors.entrySet().stream().sorted(comparing(Map.Entry::getValue, reverseOrder())).limit(5).collect(toList());
}
public int getTopByName(String name) {
for (int i = 0; i < getTops().size(); i++) {
if (getTops().get(i).getKey().contains(name)) {
return i;
}
}
return 0;
}