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 9198985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:25:08+00:00 2026-06-17T22:25:08+00:00

Why am getting a ClassCastException at (Person p2 = (Person) o2;) in overridden compare

  • 0

Why am getting a ClassCastException at (Person p2 = (Person) o2;) in overridden compare method . 🙁

Actually Instead of Person Object the values in compare overridden method is coming as “Jim” and “Jack” (Key values). So the Cast Cast Exception . But Why is it coming with keys not values i,e the Person object , Why is it only applied for keys . Are there any other way to sort it based on values .

Please correct me if am wrong

1) We can Pass the comparator object in the TreeMap which will sort it accordingly.?

2) Always the Sorting is performed over Keys . ?

3) How can we sort a Map over its values without using anymore collection object (Is it possible) and why is not supported by default ?

public class HashTableExamples {

/**
 * @param args
 */
public static void main(String[] args) {

    SortedMap persorSorted = new TreeMap(new Comparator() {

        @Override 
        public int compare(Object o1, Object o2) {
            Person p2 = (Person) o2;
            return 2;
        }
    });

    Person p = new Person(10);
    Person p1 = new Person(20);
    persorSorted.put("Jim", p);
    persorSorted.put("Jack", p1);
    Iterator sortedit = persorSorted.entrySet().iterator();
    while (sortedit.hasNext()) {
        Map.Entry pairs = (Map.Entry) sortedit.next();
        Person pw = (Person) pairs.getValue();
        System.out.println("From SortedMap : " + pw.getAge());
    }
}

public static class Person {
    Person(int agevalue) {
        this.age = agevalue;
    }

    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
  • 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-17T22:25:09+00:00Added an answer on June 17, 2026 at 10:25 pm

    Yes, TreeMap always sorts on keys.

    As to why it’s not “supported by default” — it’s because there doesn’t exist a data structure in general that supports it efficiently. It’s not supported efficiently in any programming language, because the point of a Map is to be able to look things up by their key, and sorting by values means you can’t organize the data in a way that makes it efficient to look things up by keys.

    If you must sort a Map‘s entries by value, you can use something like this:

    List<Map.Entry<Foo, Bar>> entryList = 
      new ArrayList<Map.Entry<Foo, Bar>>(map.entrySet());
    Collections.sort(entryList, new Comparator<Map.Entry<Foo, Bar>>() {
      public int compare(Map.Entry<Foo, Bar> entry1, Map.Entry<Foo, Bar> entry2) {
        return entry1.getValue().compareTo(entry2.getValue());
      }
    });
    

    Alternately, if you like, you can use an alternate comparator to compare the values if you don’t control the implementation of the value type.

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

Sidebar

Related Questions

I'm getting a class cast exception namely java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.GDApplication
I am getting this error: java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call at Main.main(Main.java:39)
I'm getting the following exception. Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer
I keep getting a ClassCastException Exception in thread main java.lang.ClassCastException: java.lang.String cannot be cast
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String I am getting this exception in follwing
getting below error after i configure MQ connection factory. java.lang.ClassCastException: com.ibm.ejs.jms.JMSQueueConnectionFactoryHandle incompatible with com.ibm.mq.jms.MQQueueConnectionFactory
I am getting this error Thread [TimerDemo(185)id=307040256] (Suspended (exception ClassCastException)) UiApplication.getUiApplication() line: 462 TimerDemoTask$1.run()
Im getting a ClassCastException that shouldnt be happening. Here is the code: TwitterApplication ta
I'm getting the error java.lang.ClassCastException: java.util.Date at avb.Test.main(Test.java:38) SimpleDateFormat(yyyy-MM-dd HH:mm:ss.SSS) from the code DateFormat
When I try to findViewById() on my custom view I keep getting a ClassCastException

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.