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.

Community
  • 1
  • 1
CobaltBabyBear
  • 2,188
  • 6
  • 26
  • 47
  • 3
    My guess would be that `Collectors.toMap` is collecting them in a hash map, destroying the ordering. – Andy Turner Jun 02 '16 at 12:19
  • That makes sense. However, that still doesn't explain the sorting of the Integer keys. – CobaltBabyBear Jun 02 '16 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. – Mark Rotteveel Jun 02 '16 at 12:28

2 Answers2

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));
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 1
    Technically, it isn't specified what kind of map `toMap` will return. The current Oracle implementation indeed uses `HashMap`. – Tunaki Jun 02 '16 at 12:26
  • 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." – Alexis C. Jun 02 '16 at 12:32
  • @AlexisC. Sorry, what do yout thik about https://stackoverflow.com/q/61844376/811293 – joseluisbz May 16 '20 at 23:35
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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243