1

I'm using the solution from this question to sort the String values in a LinkedHashMap. However the sorting simply doesn't work. Here is the code I wrote.

Map<Integer, String> sortedMap = myMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(Map.Entry<Integer, String>::getKey, 
                    Map.Entry<Integer, String>::getValue));

myMap = new LinkedHashMap<Integer, String>(sortedMap);

The weird thing is that it is sorting the Integerkeys when both comparingByValue and comparingByKey methods are used. So it definitely is sorting, just not the String values but in both cases the Integer keys. I don't understand what I'm doing wrong here.

3
  • 3
    My guess would be that Collectors.toMap is collecting them in a hash map, destroying the ordering. Commented Jun 2, 2016 at 12:19
  • That makes sense. However, that still doesn't explain the sorting of the Integer keys. Commented Jun 2, 2016 at 12:22
  • 1
    The integers seem sorted as the integer value itself is used as the hash, however as soon as you add more integers you might get a different order because of rehashing / multiple items ending up in the same bucket. Commented Jun 2, 2016 at 12:28

2 Answers 2

6

The toMap collector you are using put the elements in an HashMap, so sorting does not help here since you end up putting them in a non-ordered collection.

Use the overloaded toMap method, and supply a LinkedHashMap as concrete instance, i.e:

Map<Integer, String> sortedMap = 
     myMap.entrySet()
          .stream()
          .sorted(Map.Entry.comparingByValue())
          .collect(Collectors.toMap(Map.Entry::getKey,
                                    Map.Entry::getValue, 
                                    (a, b) -> a, //or throw an exception
                                    LinkedHashMap::new));
Sign up to request clarification or add additional context in comments.

3 Comments

Technically, it isn't specified what kind of map toMap will return. The current Oracle implementation indeed uses HashMap.
Yes, sorry. I should have said "the implementation does not make any guarantee about the properties of the map and currently uses an HashMap behind the scenes. If you need a specific implementation use the overloaded toMap method."
@AlexisC. Sorry, what do yout thik about stackoverflow.com/q/61844376/811293
2

My guess would be that Collectors.toMap is collecting them in an unordered map, immediately destroying the ordering.

Try collecting them directly in a LinkedHashMap:

LinkedHashMap<Integer, String> newMap = new LinkedHashMap<>();
Map<Integer, String> sortedMap = myMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect((k, v) -> newMap.put(k, v));
myMap = newMap;

As for why the integer keys are sorted: this is probably mere coincidence, based on how the HashMap is bucketing the keys.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.