0

I have a TreeMap and a working by key Comperator:

Map<String, Long> movieReviewsTreeMap = new TreeMap<String, Long>(new MyComperator());

class MyComperator implements Comparator<String>{
@Override
public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        }

I'm trying to modify the Comparator in order to compare TreeMap entries. My goal is to sort TreeMap by value (highest to lowest), and by key in case two values are equal.

Thanks.

Jay
  • 717
  • 11
  • 37
  • 1
    TreeMaps are by definition sorted by the keys, this is how the TreeMap in java works (and also why your comparator only sees the keys). If you want to sort by something that's not a part of the key (i.e., the value), you'll need to extract it into another structure and sort it there. – Darth Android Mar 17 '16 at 15:04

2 Answers2

0

You can sort only keys in java.util.TreeMap but you can extend it and override methods where comparator is used to implement some addiditional logic.

Javasick
  • 2,753
  • 1
  • 23
  • 35
0

You can try some what likewise, to full fill you requirement..

import java.util.*;
import java.util.Map.Entry; 

public  class TestCollection13{

    public static void main(String args[]){

        Map<MyComperator, Long> movieReviewsTreeMap = new TreeMap<MyComperator, Long>(new MyComperator());

        Long l1 = new Long(5);
        movieReviewsTreeMap.put(new MyComperator("a", l1),l1);

        Long l2 = new Long(2);
        movieReviewsTreeMap.put(new MyComperator("a", l2),l2);

        Long l3 = new Long(6);
        movieReviewsTreeMap.put(new MyComperator("a", l3),l3);

        Long l4 = new Long(0);
        movieReviewsTreeMap.put(new MyComperator("a", l4),l4);

        for(Entry<MyComperator, Long> entry : movieReviewsTreeMap.entrySet()){
            System.out.println("Key : " + entry.getKey().getKey() +  " , value :" + entry.getValue());
        }

    } 
}


class MyComperator implements Comparator<MyComperator>{

    String key;
    Long value;

    MyComperator(){

    }

    MyComperator(String key, Long value){
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }

    @Override
    public int hashCode() {
        return this.key.length();
    }

    @Override
    public boolean equals(Object obj) {

        MyComperator mc = (MyComperator)obj;

        if(mc.getKey().equals(this.getKey())  && mc.getValue().equals(this.getValue())){
            return false;
        }

        // TODO Auto-generated method stub
        return super.equals(obj);
    }

    @Override
    public int compare(MyComperator o1, MyComperator o2) {

        if(o1.getKey().equals(o2.getKey())){
            return o1.getValue().compareTo(o2.getValue());
        }

        return 0;
    }

}

Answer :

Key : a , value :0
Key : a , value :2
Key : a , value :5
Key : a , value :6
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55