英文:
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以便让它比较x1
和x2
?
注意,我将nummberOfPoints
设置为ArrayList<Pair> listOfPoints
的长度。
<details>
<summary>英文:</summary>
I'm looking for simple way to build a Priority Queue in Java. I've built an `ArrayList<Pair>`, 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<Pair> 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<String> pq=
new PriorityQueue<String>(5,(a,b) -> a.length() - b.length());
I think this is close to what I want.
I was trying to implement the following:
PriorityQueue<Pair> xSorted = new PriorityQueue<Pair>(numOfPoints, (x1,x2) -> 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<Pair> 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
英文:
For the natural (ascending) order based on xVal
:
PriorityQueue<Pair> pq= new PriorityQueue<>(Comparator.comparingDouble(Pair::getX));
For the reversed (descending) order based on xVal
:
PriorityQueue<Pair> pq= new PriorityQueue<>(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<Pair> queue = new PriorityQueue<>(5, (p1, p2) -> Float.compare(p1.getX(), p2.getX()));
For reverse order:
PriorityQueue<Pair> queue = new PriorityQueue<>(5, (p1, p2) -> 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<Pair> queue = new PriorityQueue<>(NodeUtil::customCompare);
public static int customCompare(Pair p1, Pair p2) {
return Float.compare(p1.getX(), p2.getX());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论