I have a hashmap like this:
HashMap<String,Integer> map = new HashMap<String,Integer>(); map.put("java",4); map.put("go",2); map.put("objective-c",11); map.put("c#",2);
Now I want to sort this map by key length if the two lengths of the keys are equal (e.g. go and C # as length 2) and then sorted in alphba order. so the result that I expect to get is something like:
printed result: objective-c, 11 java, 4 C #, 2 go, 2
here is my own attamp, but it doesn't work at all ...
HashMap<String,Integer> map = new HashMap<String,Integer>(); map.put("java",4); map.put("go",2); map.put("objective-c",11); map.put("c#",2); Map<String,Integer> treeMap = new TreeMap<String, Integer>( new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.length().compareTo(s2.length()); } } );
actually the compareTo method looks like red (cannot compile) .... please help me with some sample code ... I'm a little confused about how to use the comparator class to configure the comparison object ...
source share