手动实现一个树结构(根据参数ID返回当前节点以及所有子节点,递归查询下一级节点)

package com.study.utils;

import com.study.entity.Per;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class TreeUtils {

public static Per getCurrentNode(List list){
    Per per = list.stream().min(Comparator.comparing(Per::getId)).get();

    return setChildrenNode(per, list);
}
public static Per setChildrenNode(Per per,List list){
    List collect = list.stream().filter(item -> {
        return per.getId().equals(item.getPid());
    }).collect(Collectors.toList());

    per.setPerList(collect);

    if(collect.size()!=0){
        collect.stream().forEach(item -> {setChildrenNode(item,list);});
    }

    return per;
}

}

你可能感兴趣的:(手动实现一个树结构(根据参数ID返回当前节点以及所有子节点,递归查询下一级节点))