Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:00:46+00:00 2026-06-16T17:00:46+00:00

Possible Duplicate: Java: SortedMap, TreeMap, Comparable? How to use? I am using the Java

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:00:47+00:00Added an answer on June 16, 2026 at 5:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java char array to int I have the following code, where stackCells
Possible Duplicate: Java: What is the difference between implementing Comparable and Comparator? Difference between
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have
Possible Duplicate: Java Generics In Eclipse, I am given warnings for using 'rawtypes' and
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Java Oracle localhost connection error (ORA-12505) When I try to connect to
Possible Duplicate: Java - Using Jena APi - Get data from RDF file I'm
Possible Duplicate: Java abstract interface Why do Java developers use public abstract for interfaces?
Possible Duplicate: Java Generics, how to avoid unchecked assignment warning when using class hierarchy?
Possible Duplicate: Java - when to use 'this' keyword Is it generally good convention

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.