&& 操作符 使用注意——警示!!
理论都知道,运用中,会无意犯规:
(左表达式) && (右表达式) : 操作符,只有在 左表达式为 true 的情况下,才会执行 后面的 右表达式。
错误的写法:【您能发现么】
1 boolean flag = true; 2 for(ConditionBO subjectCondition : subjectConditions){ 3 Integer relationTableFieldId = subjectCondition.getRelationTableFieldId(); 4 Integer relationId = publicDataUtil.getRelationIdByRelationFieldId(relationTableFieldId); 5 Map<Integer, Boolean> dataFlagMap = relationDataFlagMapMapTemp.get(relationId); 6 if(CollectionUtils.isEmpty(dataFlagMap)){ 7 dataFlagMap = new HashMap<>(); 8 relationDataFlagMapMapTemp.put(relationId, dataFlagMap); 9 } 10 11 List<Map<String, String>> bussDataList = relationBussDataMapListMap.get(relationId); 12 List<Map<String, String>> ruleSuccessBussDataMapList = ruleSuccessBussDataMapListMapTemp.get(relationId); 13 if(CollectionUtils.isEmpty(ruleSuccessBussDataMapList)) { 14 ruleSuccessBussDataMapList = new ArrayList<>(); 15 ruleSuccessBussDataMapListMapTemp.put(relationId, ruleSuccessBussDataMapList); 16 } 17 flag = flag && this.judgeValues(flag, ruleSuccessBussDataMapList, dataFlagMap, bussDataList, subjectCondition); // 标记数据是否使用 18 // if(!flag)break; 19 }
正确的写法:
1 boolean flag = true; 2 for(ConditionBO subjectCondition : subjectConditions){ 3 Integer relationTableFieldId = subjectCondition.getRelationTableFieldId(); 4 Integer relationId = publicDataUtil.getRelationIdByRelationFieldId(relationTableFieldId); 5 Map<Integer, Boolean> dataFlagMap = relationDataFlagMapMapTemp.get(relationId); 6 if(CollectionUtils.isEmpty(dataFlagMap)){ 7 dataFlagMap = new HashMap<>(); 8 relationDataFlagMapMapTemp.put(relationId, dataFlagMap); 9 } 10 11 List<Map<String, String>> bussDataList = relationBussDataMapListMap.get(relationId); 12 List<Map<String, String>> ruleSuccessBussDataMapList = ruleSuccessBussDataMapListMapTemp.get(relationId); 13 if(CollectionUtils.isEmpty(ruleSuccessBussDataMapList)) { 14 ruleSuccessBussDataMapList = new ArrayList<>(); 15 ruleSuccessBussDataMapListMapTemp.put(relationId, ruleSuccessBussDataMapList); 16 } 17 boolean innerFlag = this.judgeValues(flag, ruleSuccessBussDataMapList, dataFlagMap, bussDataList, subjectCondition); // 标记数据是否使用 18 flag = flag && innerFlag; 19 if(!flag)break; 20 }
后续再完善!