In Java, sort hash map by its key.length()
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 its key length, if two keys length are equal (e.g go and c# both length 2), then sorted by alphba order. so the outcome 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 doesnt 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);