I have the following:
List<String> keys
List<String> values
I would like map these two lists to a Map<String, String>
using Java 8 Streams. The lists have both the same size and are sorted the same way.
I tried to map these two with the following
Map<String, String> result= keys.stream().
collect(Collectors.toMap(keys::get, values::get));
But this doesnt work at all - how can I do this correclty? Thanks in advance :)
You can iterate over the indices of the List
s with an IntStream
:
Map<String, String> result =
IntStream.range(0,keys.size())
.boxed()
.collect(Collectors.toMap(i -> keys.get(i), i -> values.get(i)));
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