引言:有时在实际项目中,可能会需要以树状结构来展示数据,并返回给前端,比如像部门的展示就有
多层级,以树状展示更清晰明了,这里以递归的方式来处理数据。
1.定义一个列表转树状工具类。
/**
* 树状工具类
*/
public class TreeUtils {
static String root = "0";
/**
* Primary key id
*/
static String id = "id";
/**
* Parent node id
*/
static String pid = "pid";
/**
* Child node collection
*/
static String children = "children";
/**
* 列表转树状结构
* @param trees
* @param Map
* @return result
* @throws IllegalAccessException result
*/
public static List<Map<String,Object>> ListToTreeMap(List<Map<String,Object>> trees) throws IllegalAccessException {
//Root node collection
List<Map<String,Object>> rootList = new ArrayList<Map<String,Object>>();
HashMap<Object, List<Map<String,Object>>> pidAndTrees= new HashMap<>();
for (Map<String,Object> t : trees) {
t.put(children, new ArrayList<Map<String, Object>>());
Object value=t.get(pid);
if(value!=null){
if(pidAndTrees.get(value)!=null) {
pidAndTrees.get(value).add(t);
}else{
pidAndTrees.put(value,new ArrayList<Map<String,Object>>(){{this.add(t);}});
}
if(Objects.equals(root, String.valueOf(value))) {
rootList.add(t);
}
}
}
buildChilTreeMap(rootList,pidAndTrees);
return rootList;
}
/**
* @param currentTrees
* @param trees
* @throws IllegalAccessException
*/
private static void buildChilTreeMap(List<Map<String,Object>> currentTrees, HashMap<Object, List<Map<String,Object>>> trees) throws IllegalAccessException {
for (Map<String,Object> t : currentTrees) {
Object currentId = t.get(id);
if(trees.get(currentId)!=null){
t.put(children, trees.get(currentId));
buildChilTreeMap(trees.get(currentId),trees);
}
}
}
}
2.定义一个测试Demo。
public class TreeDemo {
public static void main(String[] args) throws IllegalAccessException {
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
list.add(new HashMap<String, Object>(){{put("id",1);put("pid",0);}});
list.add(new HashMap<String, Object>(){{put("id",33);put("pid",34);}});
list.add(new HashMap<String, Object>(){{put("id",2);put("pid",1);}});
list.add(new HashMap<String, Object>(){{put("id",17);put("pid",1);}});
list.add(new HashMap<String, Object>(){{put("id",19);put("pid",17);}});
list.add(new HashMap<String, Object>(){{put("id",15);put("pid",2);}});
list.add(new HashMap<String, Object>(){{put("id",5);put("pid",15);}});
list.add(new HashMap<String, Object>(){{put("id",6);put("pid",15);}});
list.add(new HashMap<String, Object>(){{put("id",7);put("pid",15);}});
list.add(new HashMap<String, Object>(){{put("id",21);put("pid",15);}});
list.add(new HashMap<String, Object>(){{put("id",8);put("pid",5);}});
list.add(new HashMap<String, Object>(){{put("id",11);put("pid",5);}});
list=TreeUtils.ListToTreeMap(list);
System.err.println(list);
}
}
3.返回数据:
[{
children = [{
children = [{
children = [{
children = [{
children = [],
pid = 5,
id = 8
}, {
children = [],
pid = 5,
id = 11
}],
pid = 15,
id = 5
}, {
children = [],
pid = 15,
id = 6
}, {
children = [],
pid = 15,
id = 7
}, {
children = [],
pid = 15,
id = 21
}],
pid = 2,
id = 15
}],
pid = 1,
id = 2
}, {
children = [{
children = [],
pid = 17,
id = 19
}],
pid = 1,
id = 17
}],
pid = 0,
id = 1
}]
大家有兴趣可以试一试。