說是有其需要被第一修改,然後過濾收集:有沒有解決方法來從forEach方法「返回」流?
Collection<MyObject> collection = ...
Stream<MyObject> dummyVariable = collection.stream();
dummyVariable.forEach(i -> i.callModifyingFunc(args));
return dummyVariable.filter(i -> i.isNeeded);
因爲具有的forEach void
返回類型是不可能寫代碼而不dummyVariable
。有任何解決方法在一條鏈全部寫入:
Collection<MyObject> collection = ...
return Stream<MyObject> dummyVariable = collection.stream().forEach(i -> i.callModifyingFunc(args)).filter(i -> i.isNeeded);
[map](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map-java.util.function.Function-) – brian