Java中将Key和Value加入优先队列并按Key排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29872664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:44:24  来源:igfitidea点击:

Add Key and Value into an Priority Queue and Sort by Key in Java

javapriority-queue

提问by user3072188

I am trying to take in a List of strings and add them into a Priority Queue with Key and Value. The Key being the word and the value being the string value of the word. Then I need to sort the queue with the highest string value first. The priority queue is not letting me add 2 values.

我正在尝试接收字符串列表并将它们添加到具有键和值的优先级队列中。Key 是单词,value 是单词的字符串值。然后我需要先对具有最高字符串值的队列进行排序。优先级队列不允许我添加 2 个值。

public static List<String> pQSortStrings(List<String> strings) {
    PriorityQueue<String, Integer> q = new PriorityQueue<>();

    for (int x = 0; x < strings.size(); x++) {
        q.add(strings.get(x),calculateStringValue(strings.get(x)));
    }
    return strings;
}

采纳答案by Tanmay Patil

Problem

问题

PriorityQueuecan store a single object in it's each node. So what you are trying to do can not be done as it is.

PriorityQueue可以在它的每个节点中存储单个对象。因此,您正在尝试做的事情不能按原样完成。

But you can compose both objects in a single class and then use the PriorityQueue.

但是您可以将两个对象组合在一个类中,然后使用PriorityQueue.

You would either need to supply a Comparatoror rely on natural orderingby implementing Comparableinterface.

您要么需要提供 aComparator要么通过实现接口依赖自然排序Comparable



Solution

解决方案

  • Create a class which has Stringand intas it's members.

    public class Entry {
        private String key;
        private int value;
    
        // Constructors, getters etc.
    }
    
  • Implement Comparableinterface and delegate comparison to String.

    public class Entry implements Comparable<Entry> {
        private String key;
        private int value;
    
        public Entry(String key, int value) {
            this.key = key;
            this.value = value;
        }
    
        // getters
    
        @Override
        public int compareTo(Entry other) {
            return this.getKey().compareTo(other.getKey());
        }
    }
    
  • Build the PriorityQueueusing this class.

    PriorityQueue<Entry> q = new PriorityQueue<>();
    
  • Add elements as following.

    q.add(new Entry(strings.get(x), calculateStringValue(strings.get(x))));
    
  • 创建具有类Stringint它的成员。

    public class Entry {
        private String key;
        private int value;
    
        // Constructors, getters etc.
    }
    
  • 实现Comparable接口并将比较委托给String.

    public class Entry implements Comparable<Entry> {
        private String key;
        private int value;
    
        public Entry(String key, int value) {
            this.key = key;
            this.value = value;
        }
    
        // getters
    
        @Override
        public int compareTo(Entry other) {
            return this.getKey().compareTo(other.getKey());
        }
    }
    
  • PriorityQueue使用这个类构建。

    PriorityQueue<Entry> q = new PriorityQueue<>();
    
  • 添加元素如下。

    q.add(new Entry(strings.get(x), calculateStringValue(strings.get(x))));
    

Hope this helps.

希望这可以帮助。

回答by Ankit Sharma

Using Java-8

使用 Java-8

PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>((a, b)->b.getValue()-a.getValue());

to add a new Entry

添加新条目

queue.offer(new AbstractMap.SimpleEntry<>("A", 10));