在Java8中将循环转换为Lambda表达式

时间:2020-03-10 06:26:53

标签: java arraylist lambda java-8 java-stream

我需要帮助将代码转换为lambda表达式。

我能够将以下代码转换为lambda表达式

private final List<Envelope> toPersist; //it gets populated
final List<Request> persistables = new ArrayList<>(); //Request is an entity class
for (Envelope envelope : this.toPersist) { //
    Request requests = persistable(envelope);
    persistables.add(requests);
}

到lambda表达式下面,这很好用。

final List<Request> persistables = this.toPersist.stream().map(this::persistable).collect(toList());

(工作正常)。

但是我无法转换为包含List的以下代码。

final List<Annotation> annotations = new ArrayList<>(); //Annotation is an entity class
for (Envelope envelope : this.toPersist) {
   List<Annotation> annotationList = annotationsPersistable(envelope);
   annotations.addAll(annotationList);
}

请帮助将上述代码转换为lambda表达式。

1 个答案:

答案 0 :(得分:3)

您可以while input("Do You Want To Continue? [y/n]") in ["y", "Y"]:

flatMap
相关问题