简体   繁体   中英

Attempting to override existing comparator for PriorityQueue in Java

So I am trying to implement Dijkstra's algorithm using the priority queue data structure in java. The only thing I need to modify is the comparable operator as java cannot compare two variables of type Adjacency (user defined). I want to use the PriorityQueue data structure and all I want to do is to modify the compareTo operator so that it can compare two variables of type Adjacency. As of now the error I get is "The type PriorityQueue must implement the inherited abstract method Comparable.compareTo(Adjacency)". When I try adding the "@override" tag above the compareTo operator, I get the error "override cannot be resolved to a type".

public class Adjacency {
  public int vertex;
  public double weight;
  public Adjacency(int v, double w) {
    vertex = v;
    weight = w;
  }
}

public class PriorityQueue<T extends PriorityQueue<T>> implements Comparable<Adjacency> {


    public int compareTo(Adjacency o1, Adjacency o2) {
        return Double.compare(o1.weight, o2.weight);


    }

}

EDIT: Here is the Hello class which implements Comparator. The mistake I was making is by using Comparable because you can only compare this and another variable and cannot pass two separate variables to the class. Implementing Comparator solves that problem.

import java.util.*;

public class Hello implements Comparator<Adjacency>{

    @Override
    public int compare(Adjacency o1, Adjacency o2) {
       return Double.compare(o1.weight, o2.weight);

      }

}

Thats not how you should to it. Don't subclass PriorityQueue . The easiest thing would be to implement Comparable<Adjacency> in your Ajacency class.

In your case this could go like this:

public class Adjacency implements Comparable<Adjacency> {
    public int vertex;
    public double weight;
    public Adjacency(int v, double w) {
        vertex = v;
        weight = w;
    }

    @Override
    public int compareTo(Adjacency other) {
        return Double.compare(this.weight, other.weight);
    }
}

PriorityQueue has a constructor that supports overriding its default Comparator .

What this means is that you'd have to provide an initial capacity to the queue, but you also get to craft your own custom comparator, which is what you want, without having to modify or touch the implementation of PriorityQueue .

Unless you have a mandate that your Adjacency class be Comparable , you don't need to implement Comparable on Adjacency at all.

EDIT: Since you've brought it to our attention that you can't really modify the Adjacency class, then using a custom Comparator is likely the right choice.

Here's an example. You should check for null ; the snippet below is only for illustrative purposes.

Comparator<Adjacency> adjacencyComparator = new Comparator<Adjacency>() {

    @Override
    public int compare(Adjacency left, Adjacency right) {
        return left.weight.compareTo(right.weight);
    } 
};

You can Override Priority Queue Comparator with the following method :

    PriorityQueue<Adjacency>priorityQueue=new PriorityQueue<>(new Comparator<Adjacency>() {
        @Override
        public int compare(Adjacency adjacency, Adjacency t1) {
            return 0;
        }
    });

Simply change the Comparing style as you want...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM