Suvriti Gandhi :
I have a Project
class:
class Project {
List<Name> names;
int year;
public List<Name> getNames(){
return names;
}
}
Then I have another main function where I have a List<Project>
and have to filter that list of projects on the basis of year and get names list as the result.
Can you please tell me how to do it using java 8 lambda expressions?
Thanks
Eran :
Well, you didn't state the exact filtering condition, but assuming you wish to filter elements by a given year:
List<Name> names = projects.stream()
.filter(p -> p.getYear() == someYear) // keep only projects of a
// given year
.flatMap(p -> p.getNames().stream()) // get a Stream of all the
// Names of all Projects
// that passed the filter
.collect(Collectors.toList()); // collect to a List
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=460760&siteId=1