I was going through the same thing and I thought this could be helpful for someone looking for/will need to sort the treemap value by its instances (I wanted to sort the items by names in the list of the treemap value). I know this is a pretty old post but, hopefully it might be helpful for someone...
Here is an example.
//Implementing the Comparator interface to compare the
//Animal object by its name instance
class Animal implements Comparator<Animal> {
...
@Override
public int compare(Animal o1, Animal o2) {
return o1.getName().compareTo(o2.getName());
}
}
//instantiated a new treemap with a list value
TreeMap<String, List<Animal>> treeMap = new TreeMap<String, List<Animal>>( );
//While looping through the animals to add animal object from the list to treemap by its
//owner as a key, I called treemap key and sorted with perspective animal
for(Animal animal: animals){
key = animals.get(count).getOwner();
if(treeMap.containsKey(key)){
treeMap.get(key).add(animal);
}else {
List<Animal> animalList = new ArrayList<Animal>();
animalList.add(animal);
treeMap.put(animal.getOwner(), animalList);
}
//I am sorting the animal object
treeMap.get(key).sort(animal);
}
Please, feel free to edit if you have better options, I'd love to discover ;)