乐趣区

关于java:父子级关系处理

1. 多表父子级关系利用流来解决

private List<AddressInfo>  children;
List<AddressInfo> collect1 = addressInfo.stream().map(m1 -> {List<AddressInfo> collect = addressInfo.stream().filter(m2 -> Objects.equals(m1.getId(), m2.getPId())).collect(Collectors.toList());
m1.setChildren(collect);
return m1;
 }).collect(Collectors.toList());

阐明:m1 父级信息 m2 子级信息

2 单表父子级关系解决

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    /**
     * 3. 循环解决全副数据,转为父子级对象
     */
    for (AddressInfo vo : addressInfo) {Map<String, Object> map = new HashMap<String, Object>(8);
        if (vo.getPId() == 0) {map.put("children", getChildren(addressInfo, vo.getId()));
            map.put("pId", vo.getPId());
            寄存子级数据
            list.add(map);
        }
    }
    PageInfo<AddressInfo> pageInfo = new PageInfo<>(addressInfo);
    return success(list, pageInfo.getTotal());
}

public List<Map<String, Object>> getChildren(List<AddressInfo> data, Integer wzflid) {
    /**
     * 1. 对数据校验解决
     */
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    if (data == null || data.size() == 0 || wzflid == null) {return list;}
    /**
     * 2. 遍历匹配父子级关系数据
     */
    for (AddressInfo vo : data) {Map<String, Object> map = new HashMap<String, Object>(8);
        if (wzflid.equals(vo.getPId())) {map.put("pId", vo.getPId());
            
            list.add(map);
        }
    }
    return list;
}
退出移动版