【问题标题】:Priority queue with Comparator带比较器的优先队列
【发布时间】:2021-09-19 05:32:57
【问题描述】:

我正在使用优先级队列来存储 Identity HashMap 的元素(以便也保留重复的键)。我想根据降序的键在优先级队列中添加 IdentityHashMap 的元素并打印它们的索引(从 1 开始)。但它没有给出正确的输出。

这是我的代码:

import java.util.*;
import java.lang.*;

class Test {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        IdentityHashMap<Integer, Integer> hm = new IdentityHashMap<>();
        PriorityQueue<Map.Entry<Integer, Integer>> p = new PriorityQueue<>(new my_Comparator());
        int size = sc.nextInt();
        int a[] = new int[size];
        for (int index = 0; index < size; index++) {
            a[index] = sc.nextInt();
            hm.put(a[index], index + 1);
        }
        for (Map.Entry<Integer, Integer> temp : hm.entrySet()) {
            p.add(temp);
        }
        while (p.size() != 0) {
            Map.Entry<Integer, Integer> temp = p.poll();
            System.out.print(temp.getValue() + " ");
        }
        System.out.println();
    }
}

class my_Comparator implements Comparator<Map.Entry<Integer, Integer>> {

    public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
        Integer i1 = o1.getKey();
        Integer i2 = o2.getKey();
        if (i1.equals(i2)) {
            return o2.getValue().compareTo(o1.getValue());
        }
        return i2.compareTo(i1);
    }
}

输入:

8
19 1 8 25 20 12 4 25

正确的输出应该是这样的:

8 4 5 1 6 3 7 2

这些是元素在优先队列中按降序排序后的索引。

我的代码输出是:

8 5 1 6 3 7 2

【问题讨论】:

  • 请查看并尝试遵循 Java 代码格式规则。通过遵循这些规则,其他人将更容易阅读和理解您的代码,然后能够帮助您。如果您使用大多数 IDE,它们可以帮助您正确格式化代码。

标签: java sorting hashmap comparator priority-queue


【解决方案1】:

问题来了,因为您试图在输入中使用重复值。

for (int index = 0; index < size; index++) {
          a[index] = sc.nextInt();
          hm.put(a[index], index + 1);
      }

当第一次输入 25 时,它会保存 25 的值和输入它的一个索引。 当最后再次输入 25 时,它会更新 index.HashMap.put 的值,如果 key 已经存在,则基本上会更新值。 因此,在原始的 hashMap 中,只存在 7 个条目。

要解决您的问题, 使用索引作为键,使用值作为 hashMap 的值。 然后它会在输入 25 时存储这两个条目。 这是给出所需输出的工作代码

import java.util.*;
import java.lang.*;
import java.io.*;

public class Test{
public static void main (String[] args) {

Scanner sc=new Scanner(System.in);
IdentityHashMap<Integer,Integer> hm=new IdentityHashMap<>();
PriorityQueue<Map.Entry<Integer,Integer>>p=new PriorityQueue<>(new myComparator());
    int size=sc.nextInt();
    int a[]=new int[size];
for(int index=0;index<size;index++)
{
    a[index]=sc.nextInt();
    hm.put(index+1,a[index]);
}
//System.out.println(hm);
for(Map.Entry<Integer,Integer> temp:hm.entrySet())
{
    p.add(temp);
}
while(p.size()!=0)
{Map.Entry<Integer,Integer>temp=p.poll();
    System.out.print(  temp.getKey() + " ") ;

}
System.out.println();
}       


}
class myComparator implements Comparator<Map.Entry<Integer,Integer>>
{
    
    public int compare(Map.Entry<Integer,Integer>o1,Map.Entry<Integer,Integer> o2)
    {
    Integer i1 =o1.getValue();
    Integer i2 =o2.getValue();
    if(i1.equals(i2))
    {
         return o2.getValue().compareTo(o1.getValue());
      
    }
   
     return i2.compareTo(i1);
       
    }
   
}

【讨论】:

  • 感谢您的宝贵回答。我有点怀疑我在这里使用的 IdentityHashMap 具有保存重复键的能力,那么为什么它不存储重复键。?
  • 当你在 hashmap 中插入值时,使用 hm.put(new Integer(a[index],index+1) ; 这基本上会创建一个新键,而不是从哈希图。
  • 如果您在代码中执行注释中建议的更改,您的代码将正常工作。
猜你喜欢
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
相关资源
最近更新 更多