小编Ech*_*cho的帖子

将一个返回布尔值的嵌套for循环转换为java 8流?

我有三个类,即Engine,Wheel和AutoMobile.这些课程的内容如下: -

class Engine {
     String modelId;
 }

class Wheel {
     int numSpokes;
 }

class AutoMobile {
      String make;
      String id;
}
Run Code Online (Sandbox Code Playgroud)

我有一个List<Engine>,一个List<Wheel>和一个List<Automobile>我必须迭代并检查特定条件.如果有一个实体满足这个条件,我必须返回true; 否则该函数返回false.

功能如下:

Boolean validateInstance(List<Engine> engines, List<Wheel> wheels , List<AutoMobile> autoMobiles) {
    for(Engine engine: engines) {
        for(Wheel wheel : wheels) {
            for(AutoMobile autoMobile : autoMobiles) {
                if(autoMobile.getMake().equals(engine.getMakeId()) && autoMobile.getMaxSpokes() == wheel.getNumSpokes() && .... ) {
                    return true;
                }
            }
        }
    } 
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我到现在为止都试过这个

 return engines.stream()
        .map(engine -> wheels.stream()
          .map(wheel -> …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

3
推荐指数
1
解决办法
750
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1