English 中文(简体)
• 如何在优先地点使用配对机,然后以钥匙作为优先权来回价
原标题:How to use pair in a priority queue and then return the value using the key as the priority

因此,我想把最小的关键作为优先事项,然后回到相应的关键点:

import javafx.util.Pair;
import java.util.PriorityQueue;

public class Test
{
    public static void main (String[] args)
    {
        int n = 5;

        PriorityQueue <Pair <Integer,Integer> > l = new PriorityQueue <Pair <Integer,Integer> > (n);

        l.add(new Pair <> (1, 90));
        l.add(new Pair <> (7, 54));
        l.add(new Pair <> (2, 99));
        l.add(new Pair <> (4, 88));
        l.add(new Pair <> (9, 89));

        System.out.println(l.poll().getValue()); 
    }
}

预计产出为90个,因为1个是最小的关键。 即便价值被作为优先事项使用,而且钥匙被退回,其罚款也是因为如果我不能获得数据,我只能冲淡数据。 我想把价值/钥匙作为优先事项(在这种情况下的最低价值)显示关键/价值。 我不知道在这种情况下如何做到这一点。 该公司在C++进行罚款。

最佳回答

您需要使用<代码>Comparator,用于确定这一优先权。

使用<代码>Comparator.comparing()和Method reference在设定<代码>时比较参数 优先标准

PriorityQueue<Pair<Integer,Integer> > pq=
                new PriorityQueue<Pair<Integer,Integer>>(n, Comparator.comparing(Pair::getKey));

Or

你们可以使用伊斯兰教的表达方式。

PriorityQueue<Pair<Integer,Integer> > pq=
                    new PriorityQueue<Pair<Integer,Integer>>(n,(a,b) -> a.getKey() - b.getKey());
问题回答

替代产品:使用习惯 执行“条形状”的类别。

class Pair implements Comparable<Pair> {
    Integer value;
    Integer index;

    public Pair(Integer value, Integer index) {
        this.value = value;
        this.index = index;
    }

    @Override
    public int compareTo(Pair o) {
        return value - o.value;
    }
}

and then usage

// Add elements
queue.add(new Pair(valueKey, index));
// do it for all your elements

final Pair min = queue.poll();
Integer index = min.index;
Integer value = min.value;

// Do with them what you want.

I used PriorityQueue in leetcode challenge. https://leetcode.com/problems/merge-k-sorted-lists/discuss/630580/Using-PriorityQueue-in-Java

And this is full example https://github.com/yan-khonski-it/leetcode/blob/master/src/main/java/com/yk/training/leetcode/merge_sorted_lists/PriorityQueueSolution.java

我倾向于为此目的使用地图。 另一时间是优先建筑商的配对制。

import java.util.PriorityQueue;
import java.util.Map;
import java.util.HashMap;

public class Test
{
    public static void main (String[] args)
    {
        int n = 5;

        PriorityQueue <Map.Entry<Integer,Integer>> l = new PriorityQueue <> (n,(a,b)->Integer.compare(a.getKey(),b.getKey()));
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,90);
        map.put(7,54);
        map.put(2,99);
        map.put(4,88);
        map.put(9,89);
        l.addAll(map.entrySet());
        System.out.println(l.poll().getValue()); 
    }
}

这对我来说是有效的。

val pq = 优先权Queue<Pair<Int, Int>>compare





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签