首页 > 解决方案 > java 8中lamda表达式的使用

问题描述

我想重构这段代码以使用 lambda 表达式 java 8

for(int i = 0; i < customers.getCUSTOMER().size(); i++){
   if (customers.getCUSTOMER().get(i).getINCOME().getGROSSMONTH1().toBigInteger()
     < customers.getCUSTOMER().get(i).getINCOME().getNETTSALMONTH1().toBigInteger()){

        log.error("")
        throw new RuntimeException();
   }
}

标签: javalambdajava-stream

解决方案


customers.getCUSTOMER().stream().forEach(customer -> {
    if(customer.getINCOME().getGROSSMONTH1().toBigInteger() < customer.getINCOME().getNETTSALMONTH1().toBigInteger()){
      log.error("");
      throw new RuntimeException();
    }
});

您还应该尝试使用驼峰命名法重命名您的方法,例如 getIncome() 以使其更易于阅读并符合正常的 Java 编写标准。


推荐阅读