Java“不能转换为可比较的”当使用TreeMap时 [英] Java "cannot cast to Comparable" when using TreeMap

查看:144
本文介绍了Java“不能转换为可比较的”当使用TreeMap时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Java:SortedMap,TreeMap,Comparable?如何使用?


我使用的是Java JungI 图形包和Netbeans 7.我从Java获得以下错误:

 线程main中的异常java.lang.ClassCastException:graphvisualization.MyVertex不能转换为java.lang.Comparable 
at java.util。 TreeMap.put(TreeMap.java:542)

这里是与错误相关的代码:

  SortedMap< MyVertex,Double> vMap = new TreeMap< MyVertex,Double>(); 
double curRank = 0;
for(MyVertex v:g.getVertices())// g是SparseGraph< MyVertex,MyEdge>
{
curRank = vertexRank.getVertexScore(v);
vMap.put(v,curRank); // **这是我的错误**
}

类MyVertex是一个类为图表做。下面是MyVertex的代码

  public class MyVertex 
{
int vID; //此顶点的id
double centrality; //这个顶点的中心度度量
int degree; //这个顶点的度数

public MyVertex(int id)
{
this.vID = id;
this.centrality = 0;
this.degree = 0;
}

public double getCentrality()
{
return this.centrality;
}

public void setCentrality(double centrality)
{
this.centrality = centrality;
}

public int getDegree()
{
return this.degree;
}

public void setDegree(int deg)
{
this.degree = deg;
}

public void incrementDegree()
{
this.degree ++;
}

public void decrementDegree()
{
this.degree--;
}

@Override
public String toString()
{
returnv+ vID;
}

int compareTo(MyVertex v)
{
return(this.degree< v.degree)? 1:0; // this will do descendingly
}
}




  1. 如何获取MyVertex类型转换为Comparables?

  2. 为什么需要这样做? (我不立即看到原因)


解决方案


是否可以将MyVertex类型转换为Comparables?


实现可比较的接口。

  public class MyVertex implements Comparable< MyVertex> {

@Override
public int compareTo(Object o){
//比较逻辑在这里

}
}

或者,您可以将比较器传递给构造函数 TreeMap

 新的TreeMap< MyVertex,Double>(new Comparator< ; MyVertex>()
{
public int compare(MyVertex o1,MyVertex o2)
{
//比较逻辑在这里
}
} ;




为什么需要?


因为你存储在树映射中,这是一个排序的映射(按键排序)。地图键需要具有可比性才能确保地图中的排序顺序。


Possible Duplicate:
Java: SortedMap, TreeMap, Comparable? How to use?

I am using the Java JungI graph package and Netbeans 7. I am getting the following error from Java:

 Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable
    at java.util.TreeMap.put(TreeMap.java:542)

Here is the code associated with the error:

SortedMap<MyVertex, Double> vMap = new TreeMap<MyVertex, Double>();
       double curRank = 0;
       for(MyVertex v: g.getVertices())                 //g is a SparseGraph<MyVertex, MyEdge>
       {
           curRank = vertexRank.getVertexScore(v);
           vMap.put(v, curRank);                        //**Here is my Error**
       }

Class MyVertex is a class I made for graphs. The following is the code for MyVertex

public class MyVertex 
{
    int vID;                    //id for this vertex
    double centrality;          //centrality measure for this vertex
    int degree;                 //the degree of this vertex

    public MyVertex(int id)
    {
        this.vID = id;
        this.centrality=0;
        this.degree=0;
    }

    public double getCentrality()
    {
        return this.centrality;
    }

    public void setCentrality(double centrality)
    {
        this.centrality = centrality;
    }

    public int getDegree()
    {
        return this.degree;
    }

    public void setDegree(int deg)
    {
        this.degree = deg;
    }

    public void incrementDegree()
    {
        this.degree++;
    }

    public void decrementDegree()
    {
        this.degree--;
    }

    @Override
    public String toString()
    {
        return "v"+vID;
    }

    int compareTo(MyVertex v) 
    {
        return (this.degree < v.degree) ? 1 : 0;          //this will do descendingly
    }
}

  1. How do I get MyVertex types to be casted to Comparables?
  2. Why is this necessary? (I do not immediately see the reason)

解决方案

How do I get MyVertex types to be casted to Comparables?

Implement Comparable interface.

public class MyVertex implements Comparable<MyVertex> {

  @Override
  public int compareTo(Object o) {
   // comparison logic goes here

  }
 }

or alternately, you can pass a comparator to the constructor of TreeMap.

 new TreeMap<MyVertex, Double>(new Comparator<MyVertex>()
        {
            public int compare(MyVertex o1, MyVertex o2)
            {
                //comparison logic goes here
            } 
    });

Why is this necessary?

Because you are storing in tree map which is a sorted map (sorted by keys). Map Keys need to be comparable to ensure a sort order in the map.

这篇关于Java“不能转换为可比较的”当使用TreeMap时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆