简体   繁体   English

尝试覆盖Java中PriorityQueue的现有比较器

[英]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. 因此,我尝试使用Java中的优先级队列数据结构来实现Dijkstra的算法。 The only thing I need to modify is the comparable operator as java cannot compare two variables of type Adjacency (user defined). 我唯一需要修改的是可比运算符,因为java不能比较两个Adjacency类型的变量(用户定义)。 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. 我想使用PriorityQueue数据结构,我要做的就是修改compareTo运算符,以便它可以比较两个Adjacency类型的变量。 As of now the error I get is "The type PriorityQueue must implement the inherited abstract method Comparable.compareTo(Adjacency)". 到目前为止,我得到的错误是“类型PriorityQueue必须实现继承的抽象方法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". 当我尝试在compareTo运算符上方添加“ @override”标签时,出现错误“无法将覆盖解析为类型”。

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. 编辑:这是实现Comparator的Hello类。 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. 我犯的错误是使用Comparable,因为您只能比较此变量和另一个变量,并且不能将两个单独的变量传递给类。 Implementing Comparator solves that problem. 实施比较器可以解决该问题。

import java.util.*; 导入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 . 不要将PriorityQueue子类化。 The easiest thing would be to implement Comparable<Adjacency> in your Ajacency class. 最简单的方法是在Ajacency类中实现Comparable<Adjacency>

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 . PriorityQueue具有一个支持重写其默认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 . 这意味着您必须为队列提供初始容量,而且还可以制作自己想要的自定义比较器,而无需修改或触摸PriorityQueue的实现。

Unless you have a mandate that your Adjacency class be Comparable , you don't need to implement Comparable on Adjacency at all. 除非您具有将Adjacency类设为Comparable ,否则根本不需要在Adjacency上实现Comparable

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. 编辑:由于您已经引起我们注意,您无法真正修改Adjacency类,因此使用自定义Comparator可能是正确的选择。

Here's an example. 这是一个例子。 You should check for null ; 应该检查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... 只需根据需要更改比较样式...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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