enum evm {
and, or;
}
@Data
@Accessors(chain = true)
static class po {
Integer id;
Boolean bo;
evm evm;
Integer group;
}
public static void main(String[] args) {
List<po> list = new ArrayList<>();
// ( 1 and 4 ) or (2 and 3)
list.add(new po().setBo(true).setId(1).setEvm(evm.and).setGroup(0));
list.add(new po().setBo(true).setId(4).setEvm(evm.and).setGroup(0));
list.add(new po().setBo(false).setId(2).setEvm(evm.or).setGroup(1));
list.add(new po().setBo(false).setId(3).setEvm(evm.or).setGroup(1));
Map<Integer, List<po>> collect = list.stream().collect(Collectors.groupingBy(po::getGroup));
List<Boolean> collect1 = collect.keySet().stream().map(e -> {
List<po> pos = collect.get(e);
boolean bos = false;
for (int i = 0; i < pos.size() - 1; i++) {
//拿第一位
Boolean bo = pos.get(i).getBo();
//拿下一位
Boolean next = pos.get(i + 1).getBo();
//拿与或关系
evm evm = pos.get(i + 1).getEvm();
switch (evm) {
case or:
log.info(JSONObject.toJSONString(pos.get(i)) + " || " + JSONUtil.toJsonStr(pos.get(i + 1)));
bos = bo || next;
break;
case and:
log.info(JSONObject.toJSONString(pos.get(i)) + " && " + JSONUtil.toJsonStr(pos.get(i + 1)));
bos = bo && next;
break;
}
}
return bos;
}).collect(Collectors.toList());
System.out.println(collect1);
System.out.println(list);
}
与或运算的解析
最新推荐文章于 2023-03-10 20:05:11 发布