private List<BusinessObject> createList(int property1, List<String> filenames) {
List<BusinessObject> objectList = new ArrayList();
filenames.forEach(filename -> {
BusinessObject businessObj = parseObject(filename);
if (businessObj.getProperty1() == property1) {
objectList.add(businessObj);
}
});
return objectList;
}
我觉得那里一定有一个更加惯用的或Java 8式的解决方案,例如
filesnames.forEach(fileName -> parseObject(fileName)).(some method that takes the output of parseObject)
最佳答案
确切的流等效项为:
return filenames.stream()
.map(filename -> parseObject(filename))
.filter(o -> o.getProperty1() == property1)
.collect(Collectors.toCollection(ArrayList::new));