0

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.

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.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
sdub0800
  • 139
  • 1
  • 9
  • Hi I provided that link as a reference the code block right below it is what I found helpful in that link. What i want is a lambada expression to use in the priorirty queue knowing that I'm using the class instead of – sdub0800 Oct 11 '20 at 16:41
  • `PntPair` constructor does not compile for the `Pair` class. Please [edit](https://stackoverflow.com/posts/64306525/edit) your question and format it as well. – Giorgi Tsiklauri Oct 11 '20 at 16:45
  • Your array list should be `List listOfPoints` – Software Engineer Oct 11 '20 at 16:54

2 Answers2

1

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 API.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • Understood how to use it on was unsure how to use lambada expressions on my class . I'd like to access Pair X1 and Pair X2 and have the priority queue be sorted on the smallest value of X from that comparison. – sdub0800 Oct 11 '20 at 16:47
  • Awesome thank you! I'll give that a shot. Can Comparator run on float values? – sdub0800 Oct 11 '20 at 16:54
  • See the Comparator API link provided in the answer. There are different *comparingX* methods, so, yes, it can compare floats. – Giorgi Tsiklauri Oct 11 '20 at 17:01
0

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());
}
1218985
  • 7,531
  • 2
  • 25
  • 31