I have a Map pairs and want to turn this into an ArrayList with Pair objects.
I know i can do something like this
List<Pair<A,B>> nvpList = new ArrayList<Pair<A,B>>(2);
for(Map.Entry<String, String> entry : pairs.entrySet()){
Pair n = new Pair(entry.getKey(), entry.getValue());
nvpList.add(n);
}
How can we do this in java8 using streams?
Considering generics, you can perform that as:
<A, B> List<Pair<A, B>> convertMapToListOfPairs(Map<A, B> pairs) {
return pairs.entrySet().stream()
.map(entry -> Pair.of(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}
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