未知的比较器接口错误Java

时间:2013-11-29 07:12:54

标签: java sorting

所以我正在开发一个通用的Graph包,我正在尝试根据它们的自然顺序对我的边集进行排序,使用这种方法(我不允许更改):

 /** Returns the natural ordering on T, as a Comparator.  For
 *  example, if stringComp = Graph.<Integer>naturalOrder(), then
 *  stringComp.compare(x1, y1) is <0 if x1<y1, ==0 if x1=y1, and >0
 *  otherwise. */
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
    return new Comparator<T>() {
        @Override
        public int compare(T x1, T x2) {
            return x1.compareTo(x2);
        }
    };
}

我有一个HashSet来保存我的所有边缘,我想根据这个方法对它们进行排序, 所以我现在正在做(迭代基本上是一个迭代器包装类):

/** Returns an iterator over all edges in me. If _ordered, return an iterator 
    over the edges in their natural ordering. */
public Iteration<Edge> edges() {
    if (_ordered) {
        List<Edge> list = new ArrayList<Edge>(edges);
        Collections.sort(list, naturalOrder());
        return Iteration.iteration(list.listIterator());
    } else {
        return Iteration.iteration(edges);
    }
}

但Eclipse在.sort下给出了以下错误:

The method sort(List<T>, Comparator<? super T>) in the type Collections
is not applicable for the arguments (List<Graph<VLabel,ELabel>.Edge>,
Comparator<Comparable<? super Comparable<? super T>>>)

我真的不明白这意味着什么,也不知道如何解决它。任何人都可以摆脱一些 关于这种情况?

编辑:以下是关于该课程的更多信息(请原谅缩进):

/** Represents a general graph whose vertices are labeled with a type
 *  VLABEL and whose edges are labeled with a type ELABEL. The
 *  vertices are represented by the inner type Vertex and edges by
 *  inner type Edge.  A graph may be directed or undirected.  For
 *  an undirected graph, outgoing and incoming edges are the same.
 *  Graphs may have self edges and may have multiple edges between vertices.
 *
 *  The vertices and edges of the graph, the edges incident on a
 *  vertex, and the neighbors of a vertex are all accessible by
 *  iterators.  Changing the graph's structure by adding or deleting
 *  edges or vertices invalidates these iterators (subsequent use of
 *  them is undefined.)
 */
public abstract class Graph<VLabel, ELabel> {

/** Represents one of my vertices. */
public class Vertex {

    /** A new vertex with LABEL as the value of getLabel(). */
    Vertex(VLabel label) {
        _label = label;
    }

    /** Returns the label on this vertex. */
    public VLabel getLabel() {
        return _label;
    }

    @Override
    public String toString() {
        return String.valueOf(_label);
    }

    /** The label on this vertex. */
    private final VLabel _label;

}

/** Represents one of my edges. */
public class Edge {

    /** An edge (V0,V1) with label LABEL.  It is a directed edge (from
     *  V0 to V1) in a directed graph. */
    Edge(Vertex v0, Vertex v1, ELabel label) {
        _label = label;
        _v0 = v0;
        _v1 = v1;
    }

    /** Returns the label on this edge. */
    public ELabel getLabel() {
        return _label;
    }

    /** Return the vertex this edge exits. For an undirected edge, this is
     *  one of the incident vertices. */
    public Vertex getV0() {
        return _v0;
    }

    /** Return the vertex this edge enters. For an undirected edge, this is
     *  the incident vertices other than getV1(). */
    public Vertex getV1() {
        return _v1;
    }

    /** Returns the vertex at the other end of me from V.  */
    public final Vertex getV(Vertex v) {
        if (v == _v0) {
            return _v1;
        } else if (v == _v1) {
            return _v0;
        } else {
            throw new
                IllegalArgumentException("vertex not incident to edge");
        }
    }

    @Override
    public String toString() {
        return String.format("(%s,%s):%s", _v0, _v1, _label);
    }

    /** Endpoints of this edge.  In directed edges, this edge exits _V0
     *  and enters _V1. */
    private final Vertex _v0, _v1;

    /** The label on this edge. */
    private final ELabel _label;

}

2 个答案:

答案 0 :(得分:2)

问题似乎是Edge类没有实现Comparable<Edge>,因此编译器给出了错误。

class Edge implements Comparable<Edge>{

     public int compareTo(Edge o){
        //implement
     }
}

答案 1 :(得分:0)

有两个问题。

首先Edge应该实施Comparator<Edge>,正如其他回答者所说:

class Edge implements Comparable<Edge> {

  // ...

第二个问题是编译器不够聪明,无法推断使用它时使用哪个T naturalOrder()就像使用它一样:

Collections.sort(list, naturalOrder());  // will still give the error.

有两种解决方案:

  1. 使用临时变量:

        Comparator<? super Edge> comparator = naturalOrder();
        Collections.sort(list, comparator);
    
  2. 或明确指定通用参数:

        Collections.sort(list, MyClass.<Edge>naturalOrder());
    

    假设naturalOrder()在类MyClass中。