How does the TreeMap sort? say for example you have the following map:
TreeMap<String, Integer> treemap = new TreeMap<>();
treemap.put("lol", 1);
treemap.put("Marc", 2);
treemap.put("Jesper", 3);
Iterator ittwo = treemap.entrySet().iterator();
while (ittwo.hasNext()) {
Map.Entry pairs = (Map.Entry)ittwo.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
ittwo.remove();
}
The output of this is:
Jesper = 3
Marc = 2
lol = 1
So if its not alphabetically what is it then?
It is not only alphabetical, but it is also upper/down case sensitive.
Output:
So, if you don’t need it, you can use your custom comparator, and compare string in lower case:
Output: