5
我有一些收集List<Map<String, Object>>
,需要使用Java 8 lambda表達式進行過濾。 我將收到帶有必須應用過濾標準的標誌的JSON對象。如果沒有收到JSON對象,則不需要過濾。Java 8過濾條件並收集自定義地圖
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 9中,您可以用Map.of()替換糟糕的內部類構造。 –
@BrianGoetz很高興認識你!我在實踐中閱讀Java併發性,我喜歡它! –
@DavidPérezCabrera或不等待java-9並使用guava ImmutableMap.of() – Eugene