有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何将“for each”循环转换为Java 8“Stream”

如何将一个迭代循环“to”转换为java 8在你的身体中包含一个搜索结果的方法

for(VagasRemanecentesResponse  turaluAUnic : turaluListaA){
       listAlunoCursoResponse = alunoCursoBusiness.findByCodInstAndRgmAlun(inst, turaluAUnic.getRgmAluno());
       if (listAlunoCursoResponse.size() > 0) {
          aluCurListaB.add(new VagasRemanecentesResponse(turaluAUnic, listAlunoCursoResponse));
       }
      }

或者是一个更简单的例子,代表问题的一部分,比如不使用“for”

for(Student student: students ) {
      listAlunoCursoResponse.addAll(findByid(student.getId));
}

共 (1) 个答案

  1. # 1 楼答案

    假设findByid返回Object而不是Collection(否则必须使用flatMap):

    listAlunoCursoResponse.addAll(students.stream()
                    .map(student -> findByid(student.getId))
                    .collect(Collectors.toList()));