基于java8实现层级关系嵌套-递归

import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;

/**
 * @author :zhangyang
 * @version :1.0
 * @description:
 * @date :Created in 2020/6/10 9:54
 * @modified By:
 */
@Data
@Accessors(chain = true)
public class DemoClass {

    private String id;
    private String name;
    private String pid;
    private List children;

}
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * @author :zhangyang
 * @version :1.0
 * @description:递归
 * @date :Created in 2020/6/10 11:11
 * @modified By:
 */
@AllArgsConstructor
public class DiGui {

    /**
     * 父节点
     */
    private List root;

    /**
     * 所有节点(或不包含父节点)
     */
    private List body;


    public List returnList() {

        if (body != null && !body.isEmpty()) {
            Map map = Maps.newHashMapWithExpectedSize(body.size());
            root.forEach(entity -> getChildren(entity, map));
            return root;
        }
        return null;

    }

    public void getChildren(DemoClass demoClass, Map map) {

        List children = Lists.newArrayList();
        body.stream()
                .filter(entity -> !map.containsKey(entity.getId()))
                .filter(entity -> entity.getPid().equals(demoClass.getId()))
                .forEach(entity -> {
                    map.put(entity.getId(), entity.getPid());
                    getChildren(entity, map);
                    children.add(entity);
                });
        demoClass.setChildren(children);

    }

}
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * @author :zhangyang
 * @version :1.0
 * @description:
 * @date :Created in 2020/6/10 9:53
 * @modified By:
 */
@Slf4j
public class Demo {

    public static void main(String[] args) {

        List root = Lists.newArrayList(
                new DemoClass().setId("1").setName("1").setPid("0"),
                new DemoClass().setId("2").setName("2").setPid("0"),
                new DemoClass().setId("3").setName("3").setPid("0"),
                new DemoClass().setId("4").setName("4").setPid("0")
        );

        List body = Lists.newArrayList(
                new DemoClass().setId("1").setName("1").setPid("0"),
                new DemoClass().setId("2").setName("2").setPid("0"),
                new DemoClass().setId("3").setName("3").setPid("0"),
                new DemoClass().setId("4").setName("4").setPid("0"),
                new DemoClass().setId("11").setName("1>1").setPid("1"),
                new DemoClass().setId("12").setName("1>2").setPid("1"),
                new DemoClass().setId("21").setName("2>1").setPid("2"),
                new DemoClass().setId("31").setName("3>1").setPid("3")
        );

        DiGui digui = new DiGui(root,body);

        List list = digui.returnList();

        log.info("JSON"+JSONObject.toJSON(list));

    }

}

 

你可能感兴趣的:(基于java8实现层级关系嵌套-递归)