3

i want to add objects to priority queue by using specified value like this

PriorityQueue<Edge> queue=new PriorityQueue<Edge>();

this is class Edge which i want to sort in priority queue by its weight

public class Edge {
private int start,end;
private double weight;

public Edge(int s, int e,Double w){
    start=s;
    end=e;
    weight=w;
}

public int getStart(){
    return start;
}

public int getEnd(){
    return end;
}

public double getWeight(){
    return weight;
}
3
  • possible duplicate of Java: How do I use a PriorityQueue? Commented May 16, 2015 at 9:06
  • i am not familiar with Comparator what that mean ? PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator); Commented May 16, 2015 at 9:16
  • I posted it as an answer. Commented May 16, 2015 at 9:21

4 Answers 4

5

You should create your priority Queue a little bit different by specifying how its elements should be compared. That is done by passing an anonymous Comparator for the Edge class:

PriorityQueue<Edge> queue=new PriorityQueue<Edge>(10, new Comparator<Edge>() {
    public int compare(Edge edge1, Edge edge2) {
        if (edge1.getWeight() < edge2.getWeight()) return -1;
        if (edge1.getWeight() > edge2.getWeight()) return 1;
        return 0;
    }
});

Maybe you will have to switch the returns of -1 and 1 depending on your sorting order.

Sign up to request clarification or add additional context in comments.

2 Comments

this answer saved me thanks a lot, But i have only one question what 10 means ?
the 10 refers to the initial capacity of the priority queue, if you already know how many edges you are going to insert, choose that number instead of 10 - otherwise 10 will be fine. it will not take up much space if you put in less than 10 and it will grow if you put more in.
0

You can create a priority queue and a Checker (custom) class that compare the edges. like this :

  class Checker implements Comparator<Edge>
  {
      public int compare(Edge ob1, Edge ob2)
      {
          if (ob1.getWeight() > ob2.getWeight()) return 1;
          else                                   return -1;
      }
  }

 //Declared priority queue
  PriorityQueue<Edge> queue=new PriorityQueue<Edge>(5, new Checker());

Comments

0

Java 8 :

PriorityQueue<Edge> queue = new PriorityQueue<>((edge1, edge2) -> {
            if (edge1.getWeight() < edge2.getWeight()) {
                return -1;
            }
            if (edge1.getWeight() > edge2.getWeight()) {
                return 1;
            }
            return 0;
});

Comments

0

Solution 1:

Min Heap:

a -> a.getWeight()

Max Heap:

a -> -a.getWeight()

Example:

PriorityQueue<Edge> PriorityQueue = new PriorityQueue<>(Comparator.comparingDouble(a -> a.getWeight()));
PriorityQueue = new PriorityQueue<>(Comparator.comparingDouble(a -> -a.getWeight()));

Solution 2:

Min Heap:

Edge::getWeight

Max Heap:

(Edge::getWeight).reversed()

Example:

PriorityQueue<Edge> PriorityQueue = new PriorityQueue<>(Comparator.comparingDouble(Edge::getWeight));
PriorityQueue = new PriorityQueue<>(Comparator.comparingDouble(Edge::getWeight).reversed());

Solution 3:

Min Heap:

(a, b) -> a.getStart() - b.getStart()

(a, b) -> (int) (a.getWeight() - b.getWeight())

Max Heap:

(a, b) -> b.getStart() - a.getStart()

(a, b) -> (int) (b.getWeight() - a.getWeight())

Example:

PriorityQueue<Edge> PriorityQueue = new PriorityQueue<>((a, b) -> a.getStart() - b.getStart());
PriorityQueue = new PriorityQueue<>((a, b) -> (int) (a.getWeight() - b.getWeight()));
PriorityQueue = new PriorityQueue<>((a, b) -> b.getStart() - a.getStart());
PriorityQueue = new PriorityQueue<>((a, b) -> (int) (b.getWeight() - a.getWeight()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.