I am trying to sort the map by String values with predicted. For example, I have the next values in my map
message,
message_message1_message2,
message1_message2,
I wanna sort my map by _ symbols in values String. The result should be like this:
message_message1_message2,
message1_message2,
message
I am trying this way:
myMap.entrySet().stream().sorted((e1, e2) -> {
String[] firstCompareValue = e1.getKey().split("_");
String[] secondCompareValue = e2.getKey().split("_");
return Integer.compare(firstCompareValue.length, secondCompareValue.length);
}).collect(Collectors.toMap(Map.Entry::getKey, Function.identity()));
and I've got next notification
Redundant 'sorted' call: subsequent 'toMap' call doesn't depend on the sort order
What am I doing wrong? How it can be done?