Sumit :
The question title may seem to be same as some other post but the content is different. So please don't mark it duplicate.
Problem:
I have the below class:
public class SCDTO extends RDTO {
private List<String> sCPairs = Collections.emptyList();
public SCDTO(List<String> sCPairs) {
this.sCPairs = sCPairs;
}
//Getter setter
}
I am trying to using the below lambda expression to set the sCPairs
.
sCPairsObject.setSCPairs(util.getSCMap().entrySet().stream().
filter(entry -> entry.getValue().contains("abc")).collect(Collectors.toCollection(ArrayList<String>::new)));
But I have an compilation error saying:
no instance(s) of type variable(s) exist so that Entry<String, List<String>> conforms to String
util.getSCMap
returns Map<String, List<String>>
.
Can anyone please explain why this is happening and how to solve it?
Thanks.
Adriaan Koster :
You are streaming entries from the map:
sCPairsObject.setSCPairs(util.getSCMap().entrySet().stream()
Then filtering out some of them:
.filter(entry -> entry.getValue().contains("abc"))
now you need to map the entries to List, for example:
.map(entry -> entry.getValue())
and stream the contents of all these lists as one stream:
.flatMap(List::stream)
Finally collect the values into a list:
.collect(Collectors.toList());