java 数据行转树 多字段级联

阅读更多

工具类 百度了半天没找到满意的 所以自己写了一个 

 

java 数据行转树 多字段级联_第1张图片java 数据行转树 多字段级联_第2张图片

/**
 * Ztree节点数据DTO
 */
@Data
@Builder
public class ZTreeNodeDto {
    private String id;
    private String name;
    private boolean open;
    private boolean isParent;
    private Object data;
    private List children;
}

 

import com.redphase.dto.ZTreeNodeDto;
import com.redphase.framework.util.ReflectUtil;
import com.redphase.framework.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class MultiTree {
    String pkAttrId;
    String[] attrIds;
    Map attrMap;
    List dataList;
    List treeList;

    public MultiTree(String pkAttrId, List dataList, String... attrIds) {
        this.pkAttrId = pkAttrId;
        this.dataList = dataList;
        this.attrIds = attrIds;
        this.attrMap = new HashMap<>();
        this.treeList = new ArrayList<>();
        if (attrIds == null || attrIds.length < 2) {
            throw new RuntimeException("必须至少一组父子结构字段!");
        }
    }

    public List buildTree() {
        for (Object data : dataList) {
            String parentKey = "";
            for (int i = 0; i < attrIds.length - 1; i++) {
                String pkId = "" + ReflectUtil.getValueByFieldName(data, pkAttrId);
                String parentAttrId = attrIds[i];
                String parentValue = "" + ReflectUtil.getValueByFieldName(data, parentAttrId);
                if (ValidatorUtil.notEmpty(parentValue)) {
                    parentKey += "/" + parentAttrId + "=" + parentValue;
                    ZTreeNodeDto parentZTreeNodeDto = attrMap.get(parentKey);
                    if (parentZTreeNodeDto == null) {
                        parentZTreeNodeDto = ZTreeNodeDto.builder().id(pkId).data(i).name(parentValue).children(new ArrayList<>()).build();
                        parentZTreeNodeDto.setParent(true);
                        attrMap.put(parentKey, parentZTreeNodeDto);
                        if (i == 0) {
                            treeList.add(parentZTreeNodeDto);
                        }
                    }
                    String childAttrId = attrIds[i + 1];
                    String childValue = "" + ReflectUtil.getValueByFieldName(data, childAttrId);
                    if (ValidatorUtil.notEmpty(childValue)) {
                        String childKey = parentKey + "/" + childAttrId + "=" + childValue;
                        ZTreeNodeDto childZTreeNodeDto = attrMap.get(childKey);
                        if (childZTreeNodeDto == null || (i + 1==attrIds.length - 1)) {
                            childZTreeNodeDto = ZTreeNodeDto.builder().id(pkId).name(childValue).children(new ArrayList<>()).build();
                            attrMap.put(childKey, childZTreeNodeDto);
                            parentZTreeNodeDto.getChildren().add(childZTreeNodeDto);
                            if (i + 1 == attrIds.length - 1) {
                                childZTreeNodeDto.setData(data);
                            } else {
                                childZTreeNodeDto.setData(i+1);
                                childZTreeNodeDto.setParent(true);
                            }
                        }
                    }
                }
            }
        }
        return treeList;
    }
}

 

调用方式 

 

   public static void main(String[] args) throws Exception {
        try {
            List dataList = .....;
            MultiTree multiTree = new MultiTree("id", dataList, "provinceName","cityName","countyName","enterprise","substation","voltageLevel","concentratorName","deviceName");
            List treeNode = multiTree.buildTree();
            System.out.println(JSON.toJSONString(treeNode));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

  • java 数据行转树 多字段级联_第3张图片
  • 大小: 24.7 KB
  • java 数据行转树 多字段级联_第4张图片
  • 大小: 14.1 KB
  • 查看图片附件

你可能感兴趣的:(java 数据行转树 多字段级联)