Skip to content
Advertisement

Get the last element of each entry in a LinkedHashMap?

I have a map and I want to extract the last value for each key from that map, but I can’t get it:

JavaScript

Extract:

JavaScript

i.e. the last NUM entries for each key

Advertisement

Answer

You can use reduce to extract the last value from Map<String,String> and then collect them using toMap into LinkedHashMap to save the insertion order

But as a note to preserve the order you have to use LinkedHashMap for input and output as well

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement