【发布时间】:2016-12-21 18:11:14
【问题描述】:
我有一些集合 List<Map<String, Object>> 需要使用 Java 8 lambda 表达式进行过滤。
我将收到带有必须应用过滤条件的标志的 JSON 对象。如果没有收到 JSON 对象,则不需要过滤。
protected List<Map<String, Object>> populate(List<SomeObject> someObjects, String string) {
taskList.stream()
// How to put condition here? Ho to skip filter if no filter oprions are received?
.filter(someObject -> (if(string != null) someobject.getName == string))
// The second problem is to collect custom map like
.collect(Collectors.toMap("customField1"), someObject.getName()) ... // I need somehow put some additional custom fields here
}
现在我正在收集这样的自定义地图:
Map<String, Object> someMap = new LinkedHashMap<>();
someMap.put("someCustomField1", someObject.field1());
someMap.put("someCustomField2", someObject.field2());
someMap.put("someCustomField3", someObject.field3());
【问题讨论】:
标签: java lambda filter java-8 java-stream