package com.nuvole.util; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; /** * @author ChenLong * @version 1.0 * @ClassName TreeUtilTest * @date 2019/4/23 19:04 */ @Slf4j public class TreeUtilTest { @Test public void convert2Tree() throws Exception { ArrayList list = new ArrayList(); //list.add(new Node(1L, 0L, "1", "")); list.add(new Node(2L, 1L, "1.1", "")); list.add(new Node(3L, 1L, "1.2", "")); list.add(new Node(4L, 3L, "1.2.1", "")); HashMap mapping = new HashMap<>(); mapping.put("byReplace", "label"); log.info(JSON.toJSONString(TreeUtil.convert2Tree(list ,""))); log.info(JSON.toJSONString(TreeUtil.convert2Tree(list ,mapping))); } class Node{ public Node(Long id,Long pid,String name,String byReplace) { this.id = id; this.pid = pid; this.name = name; this.byReplace = byReplace; } private Long id; private Long pid; private String name; private String byReplace; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getByReplace() { return byReplace; } public void setByReplace(String byReplace) { this.byReplace = byReplace; } } }