public class Node {
private String id = "";
private String name = "";
private String pid = "";
private List<Node> children = null;
}
private static List<Node> formatTree(List<Node> data) {
List<Node> res = new ArrayList<>();
for(Node m: data) {
if(StringUtils.isEmpty(m.getPid())) {
res.add(recurive(m, data));
}
}
return res;
}
private static Node recurive( Node node, List<Node> data) {
for(Node mc: data) {
if(StringUtils.equals(mc.getPid(), node.getId())) {
if(node.getChildren() == null) {
node.setChildren(new ArrayList<Node>());
}
node.getChildren().add(recurive(mc, data));
}
}
return node;
}