How can I convert this:
return this.subjects.entrySet()
.stream()
.collect(Collectors.toMap(e -> getArtistryCopy(e.getKey()), Map.Entry::getValue));
To return a LinkedHashMap instead of a map?
If you need to know, this.subjects
is a LinkedHashMap<AbstractArtistries, ArrayList<AbstractCommand>>
. AbstractArtistry and command are two custom objects I made. I need the order to be maintained.
getArtistryCopy()
returns a copy of an AbstractArtistry (which is the key).
You can use the overload of Collectors.toMap
that accepts a Supplier
for the Map
. It also takes a merge
function that how to resolve collisions between duplicate keys.
return this.subjects.entrySet()
.stream()
.collect(Collectors.toMap(e -> getArtistryCopy(e.getKey()),
Map.Entry::getValue,
(val1, val2) -> yourMergeResultHere,
LinkedHashMap::new));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With