父子级关系处理

1.多表父子级关系利用流来处理

private List  children;
List collect1 = addressInfo.stream().map(m1 -> {
List 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> list = new ArrayList>();
    /**
     * 3.循环处理全部数据,转为父子级对象
     */
    for (AddressInfo vo : addressInfo) {
        Map map = new HashMap(8);
        if (vo.getPId() == 0) {
            map.put("children", getChildren(addressInfo, vo.getId()));
            map.put("pId", vo.getPId());
            存放子级数据
            list.add(map);
        }
    }
    PageInfo pageInfo = new PageInfo<>(addressInfo);
    return success(list, pageInfo.getTotal());
}

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

你可能感兴趣的:(java)