如何简化这个 for 循环?
for (CentreServiceMainSdChildSpecialty centreServiceMainSdChildSpecialty : centreServiceMainSd.getChildSpecialties()){
centreServiceMainSdChildSpecialty.setLanguageCode(translationRequest.getTargetLanguage());
centreServiceMainSdChildSpecialty.setPublishFlag(false);
centreServiceMainSdChildSpecialty.setStatus(Constant.WORKFLOW_PENDING_TRANSLATION);
}
我想做成最简单的形式
centreServiceMainSd.getChildSpecialties().forEach { centreServiceMainSdChildSpecialty in
centreServiceMainSdChildSpecialty.setLanguageCode(translationRequest.getTargetLanguage())
centreServiceMainSdChildSpecialty.setPublishFlag(false)
centreServiceMainSdChildSpecialty.setStatus(Constant.WORKFLOW_PENDING_TRANSLATION)
}
这段代码实现了与原来的for循环相同的结果,但是更加简洁易读。
forEach
方法遍历getChildSpecialties()
数组中的每个元素,并对每个元素执行指定的操作。