我还在学习兰博达,如果我做错了什么,请原谅。
final Long tempId = 12345L;
List<Entry> updatedEntries = new LinkedList<>();
for (Entry entry : entryList) {
    entry.setTempId(tempId);
    updatedEntries.add(entityManager.update(entry, entry.getId()));
}
//entryList.stream().forEach(entry -> entry.setTempId(tempId));forEach似乎只能执行一条语句。它不会返回更新的流或函数来进一步处理。我可能选错了一个。
有人能指导我如何有效地做到这一点吗?
还有一个问题,
public void doSomething() throws Exception {
    for(Entry entry: entryList){
        if(entry.getA() == null){
            printA() throws Exception;
        }
        if(entry.getB() == null){
            printB() throws Exception;
        }
        if(entry.getC() == null){
            printC() throws Exception;
        }
    }
}
    //entryList.stream().filter(entry -> entry.getA() == null).forEach(entry -> printA()); something like this?如何将此转换为Lambda表达式?
发布于 2015-06-30 05:47:56
忘记与第一个代码片段相关。我根本不会使用forEach。由于您正在将Stream的元素收集到一个List中,所以用collect结束Stream处理会更有意义。然后需要peek来设置ID。
List<Entry> updatedEntries = 
    entryList.stream()
             .peek(e -> e.setTempId(tempId))
             .collect (Collectors.toList());对于第二个片段,forEach可以执行多个表达式,就像任何lambda表达式一样:
entryList.forEach(entry -> {
  if(entry.getA() == null){
    printA();
  }
  if(entry.getB() == null){
    printB();
  }
  if(entry.getC() == null){
    printC();
  }
});但是(查看您的注释尝试),您不能在这个场景中使用筛选器,因为如果您这样做,您将只处理一些条目(例如,entry.getA() == null中的条目)。
发布于 2017-06-20 08:35:57
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
//lambda
//Output : A,B,C,D,E
items.forEach(item->System.out.println(item));
//Output : C
items.forEach(item->{
    System.out.println(item);
    System.out.println(item.toLowerCase());
  }
});发布于 2015-06-30 06:15:26
您不必将多个操作插入到一个流/lambda中。考虑将它们分成两个语句(使用toList()的静态导入):
entryList.forEach(e->e.setTempId(tempId));
List<Entry> updatedEntries = entryList.stream()
  .map(e->entityManager.update(entry, entry.getId()))
  .collect(toList());https://stackoverflow.com/questions/31130457
复制相似问题