Java优先队列与ArrayList和Pair

huangapple go评论74阅读模式
英文:

Java Priority Queue with ArrayLists and Pairs

问题

我正在寻找在Java中构建优先级队列的简单方法我已经建立了一个`ArrayList<Pair>`,每个`Pair`实例包含一个`X``Y`

```java
class Pair {
    private final Float xVal;
    private final Float yVal;

    public Pair(Float aXVal, Float aYVal) {
        xVal = aXVal;
        yVal = aYVal;
    }

    public float getX() {
        return xVal;
    }

    public float getY() {
        return yVal;
    }
}

我的ArrayList看起来像这样:

ArrayList<Pair> listOfPoints;

使用ArrayList listOfPoints,我想要构建两个优先级队列:

  • 一个按照x值从小到大排序。
  • 一个按照y值从小到大排序。

我在寻找一种使用Lambda表达式来实现这个的简单方法。

我查看了这个在Stack Overflow上的问题,我找到了这段代码:

PriorityQueue<String> pq =
                    new PriorityQueue<String>(5, (a, b) -> a.length() - b.length());

我认为这离我想要的很近。

我尝试实现以下内容:

PriorityQueue<Pair> xSorted = new PriorityQueue<Pair>(numOfPoints, (x1, x2) -> 需要在这里提供帮助);

我如何访问Pair以便让它比较x1x2

注意,我将nummberOfPoints设置为ArrayList<Pair> listOfPoints的长度。


<details>
<summary>英文:</summary>

I&#39;m looking for simple way to build a Priority Queue in Java. I&#39;ve built an `ArrayList&lt;Pair&gt;`, and each `Pair` instance contains an `X` and `Y` values.

```java
class Pair {
    private final Float xVal;
    private final Float yVal;

    public Pair(Float aXVal, Float aYVal) {
        xVal = aXVal;
        yVal = aYVal;
    }

    public float getX() {
        return xVal;
    }

    public float getY() {
        return yVal;
    }
}

My ArrayList looks like:

ArrayList&lt;Pair&gt; listOfPoints;

Using the ArrayList listOfPoints, I wanted to build two priority queues:

  • One that is sorted on the x Values from low to high.
  • One that is sorted on the y Values form low to high.

I was looking for a simple way to do this using Lambda expressions.

I did look at this question on Stack Overflow, and I found this code:

PriorityQueue&lt;String&gt; pq=
                    new PriorityQueue&lt;String&gt;(5,(a,b) -&gt; a.length() - b.length());

I think this is close to what I want.

I was trying to implement the following:

PriorityQueue&lt;Pair&gt; xSorted = new PriorityQueue&lt;Pair&gt;(numOfPoints, (x1,x2) -&gt; Need Help Here);

How do I access Pair in order to have it compare x1 and x2?

Note, that nummberOfPoints I was setting to the length of ArrayList&lt;Pair&gt; listOfPoints.

答案1

得分: 1

自然(升序)基于 xVal 的顺序:

PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingDouble(Pair::getX));

反转(降序)基于 xVal 的顺序:

PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingDouble(Pair::getX).reversed());

您可以使用相同的方法来处理 yVal 或任何其他可比较的字段,通过使用 Comparator API

英文:

For the natural (ascending) order based on xVal:

PriorityQueue&lt;Pair&gt; pq= new PriorityQueue&lt;&gt;(Comparator.comparingDouble(Pair::getX));

For the reversed (descending) order based on xVal:

PriorityQueue&lt;Pair&gt; pq= new PriorityQueue&lt;&gt;(Comparator.comparingDouble(Pair::getX).reversed());

You can use the same approach for yVal or any other comparable field, by using Comparator<T> API.

答案2

得分: 0

你还可以像这样使用lambda,如果你不想让Pair类实现Comparator接口:

PriorityQueue<Pair> queue = new PriorityQueue<>(5, (p1, p2) -> Float.compare(p1.getX(), p2.getX()));

要进行逆序排列:

PriorityQueue<Pair> queue = new PriorityQueue<>(5, (p1, p2) -> Float.compare(p2.getX(), p1.getX()));

如果你更喜欢在静态工厂方法或实用函数中使用比较逻辑,可以使用类似这样的方式:

PriorityQueue<Pair> queue = new PriorityQueue<>(NodeUtil::customCompare);

public static int customCompare(Pair p1, Pair p2) {
  return Float.compare(p1.getX(), p2.getX());
}
英文:

You can also use lambda like this if you don't want Pair class to implement Comparator interface

PriorityQueue&lt;Pair&gt; queue = new PriorityQueue&lt;&gt;(5, (p1, p2) -&gt; Float.compare(p1.getX(), p2.getX()));

For reverse order:

PriorityQueue&lt;Pair&gt; queue = new PriorityQueue&lt;&gt;(5, (p1, p2) -&gt; Float.compare(p2.getX(), p1.getX()));

If you prefer your comparison logic in a static factory method or utility function, you can use something like this:

PriorityQueue&lt;Pair&gt; queue = new PriorityQueue&lt;&gt;(NodeUtil::customCompare);

public static int customCompare(Pair p1, Pair p2) {
  return Float.compare(p1.getX(), p2.getX());
}

huangapple
  • 本文由 发表于 2020年10月12日 00:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定