1

Starting with a map like:

Map<Integer, String> mapList = new HashMap<>();
    mapList.put(2,"b");
    mapList.put(4,"d");
    mapList.put(3,"c");
    mapList.put(5,"e");
    mapList.put(1,"a");
    mapList.put(6,"f");

I can sort the map using Streams like:

    mapList.entrySet()
    .stream()
    .sorted(Map.Entry.<Integer, String>comparingByKey())
    .forEach(System.out::println);

But I need to get list (and a String) of the correspondent sorted elements (that would be: a b c d e f) that do correspond with the keys: 1 2 3 4 5 6.

I cannot find the way to do it in that Stream command.

Thanks

As @MA says in his comment I need a mapping and that is not explained in this question: How to convert a Map to List in Java?

So thank you very much @MA

Sometimes people are too fast into closing questions!

M A
  • 71,713
  • 13
  • 134
  • 174
Jorge M. Nures
  • 680
  • 1
  • 14
  • 30
  • 3
    Look at `map` method to convert entry to value and `collect` to dump it into list. – talex Mar 05 '21 at 12:47
  • `mapList.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.mapping(Entry::getValue, Collectors.toList()))` – M A Mar 05 '21 at 12:51
  • Thank very much you @MA That is exactly what I was looking for. I could not figure out how to use collect for mapping at the Entry level and I do not see how that is explained in the question that closed mine.... https://stackoverflow.com/questions/1026723/how-to-convert-a-map-to-list-in-java – Jorge M. Nures Mar 05 '21 at 13:19
  • Indeed the duplicate is not really the best, reopened and added my comment as answer. – M A Mar 05 '21 at 13:23
  • Related: [Java 8 stream map to list of keys sorted by values](https://stackoverflow.com/questions/30425836/java-8-stream-map-to-list-of-keys-sorted-by-values), you'd just have to switch `getKey` and `getValue` around from that answer – Lino Mar 05 '21 at 13:24

3 Answers3

2

You can use a mapping collector:

var sortedValues = mapList.entrySet()
                          .stream()
                          .sorted(Map.Entry.comparingByKey())
                          .collect(Collectors.mapping(Entry::getValue, Collectors.toList()))
M A
  • 71,713
  • 13
  • 134
  • 174
  • For simplicity I would even use `.map(Entry::getValue).collect(Collectors.toList())`. The `mapping()` collector should rather be used for [downstream collecting](https://stackoverflow.com/a/36758340/5515060) – Lino Mar 05 '21 at 13:26
  • @Lino Yes that would also work but I'm not sure if there is a difference in the underlying implementation. – M A Mar 05 '21 at 13:34
1

You could also use some of the different collection classes instead of streams:

List<String> list = new ArrayList<>(new TreeMap<>(mapList).values());

The downside being that if you do all that in a single line it can get quite messy, quite fast. Additionally you're throwing away the intermediate TreeMap just for the sorting.

Lino
  • 19,604
  • 6
  • 47
  • 65
0

If you want to sort on the keys and collect only the values, you need to use a mapping function to only preserve the values after your sorting. Afterwards you can just collect or do a foreach loop.

mapList.entrySet()
       .stream()
       .sorted(Map.Entry.comparingByKey())
       .map(Map.Entry::getValue)
       .collect(Collectors.toList());
Yoni
  • 1,370
  • 7
  • 17