marti6addams :
I'm trying to use java 8 to solve the following issue. Say I have the following (A
and B
are custom classes)
ArrayList<A> skills;
HashSet<B> workCenters;
What I need to do is to find whether value a.getDepartment()
which is a String
also contained in B
which also has a method String getDepartment()
and then to collect those into new List<A>
.
I tried such:
List<A> collect = skills.stream()
.filter(s -> workCenters.contains(s.getDepartment())
.collect(Collectors.toList());
but in this case i don't do it right because I couldn't retrieve getDepartment()
from workCenters
. What would be the correct solution?
ernest_k :
You could start with converting HashSet<B>
to HashSet<String>
and then use your code:
Set<String> bDeps = workCenters.stream()
.map(B::getDepartment)
.collect(Collectors.toSet());
List<A> collect = skills.stream()
.filter(s -> bDeps.contains(s.getDepartment()))
.collect(Collectors.toList());