Use the below tree map:
Map<String, String> sortedMap = new TreeMap<>(Comparator.comparingInt(String::length)
.thenComparing(Function.identity()));
Whatever you put in this sortedMap, it will be sorted automatically. First of all, TreeMap
is sorted implementation of Map
Interface.
There is a but as it sorts keys in the natural order fashion. As the Java documentation says, String
type is a lexicographic natural order type. Imagine the below list of numbers with the String type. It means the below list will not be sorted as expected.
List<String> notSortedList = List.of("78","0", "24", "39", "4","53","32");
If you just use the default TreeMap
constructor like below and push each element one-by-one like below:
Map<String, String> map = new TreeMap<>();
for (String s : notSortedList) {
map.put(s, s);
}
System.out.println(map);
The output is: {0=0, 14=14, 24=24, 32=32, 39=39, 4=4, 48=48, 53=53, 54=54, 78=78}
As you see, number 4, for example, comes after '39'. This is the nature of the lexicographic data types, like String. If that one was an Integer data type then that was okay though.
To fix this, use an argument to first check the length of the String and then compare them. In Java 8 it is done like this:
Map<String, String> sortedMap = new TreeMap<>(Comparator.comparingInt(String::length)
.thenComparing(Function.identity()));
It first compares each element by length then apply check by compareTo
as the input the same as the element to compare with.
If you prefer to use a more understandable method, the above code will be equivalent with the below code:
Map<String, String> sortedMap = new TreeMap<>(
new Comparator() {
@Override
public int compare(String o1, String o2) {
int lengthDifference = o1.length() - o2.length();
if (lengthDifference != 0)
return lengthDifference;
return o1.compareTo(o2);
}
}
);
Because the TreeMap
constructor accepts the comparator Interface, you can build up any an even more complex implementation of Composite classes.
This is also another form for a simpler version.
Map<String,String> sortedMap = new TreeMap<>(
(Comparator<String>) (o1, o2) ->
{
int lengthDifference = o1.length() - o2.length();
if (lengthDifference != 0)
return lengthDifference;
return o1.compareTo(o2);
}
);