I have a Project class:
class Project {
List names;
int year;
public List getNames(){
return names;
}
}
Then I have another main function where I have a List 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
解决方案
Well, you didn't state the exact filtering condition, but assuming you wish to filter elements by a given year:
List 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