SortedMap with ClassCast Comparator exception

Why are you getting a ClassCastException in (Person p2 = (Person) o2;) in your overridden comparison method. :(

In fact, instead of a Person object, the values ​​in the compared overridden method come as "Jim" and "Jack" (key values). Thus, Cast Cast Exception. But why does this happen with keys and not i, e values ​​of the Person object, why it only applies to keys. Is there any other way to sort based on values.

Please correct me if I am wrong

1) We can pass a comparator object to the TreeMap, which will sort it accordingly.?

2) Sorting is always performed by keys.

3) How can we sort the map by its values ​​without using more collection object (is it possible) and why is it 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;
    }
}

      

+3


source to share


2 answers


Yes, it TreeMap

always sorts by key.

As for why it is not "supported by default", it is because in general there is no data structure that supports it efficiently. It is not supported efficiently in any programming language, because the point is Map

to be able to look up things by their key, and sorting by value means you cannot organize the data in a way that effectively looks things up by keys.

If you must sort the records Map

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());
  }
});

      

Alternatively, if you like, you can use an alternate comparator to compare values ​​if you have no control over the implementation of the value type.

+1


source


If you look at the documentation for TreeMap , you will see:

Creates a new empty treemap, ordered according to the given comparator. All keys inserted into the map must be mutually comparable to the given comparator: comparator.compare (k1, k2) must not throw a ClassCastException for any k1 and k2 keys on the map. If the user tries to place a key in a map that violates this constraint, calling put (Object key, Object value) will throw a ClassCastException.



The main thing is that it compares keys , but you are inserting the key (i.e. a String

) into Person

.

+1


source







All Articles