最近的工作有遇到此需求,如權(quán)限樹狀結(jié)構(gòu)。因?yàn)閿?shù)據(jù)庫(kù)采用的MySQL,其在語義層面對(duì)于樹狀結(jié)構(gòu)的查詢支持偏弱早芭。查詢了諸多資料,個(gè)人感覺靠譜的不多遂萌生了自己寫一個(gè)的想法诅蝶。話不多說啊退个,直接貼代碼:
package tree;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.builder.EqualsBuilder;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 樹形結(jié)構(gòu)解析
*
* @author gewx
**/
public final class Tree {
/**
* 解析數(shù)據(jù)結(jié)構(gòu)
*
* @author gewx
* @param nodeList 數(shù)據(jù)節(jié)點(diǎn)集合
* @return 解析完成后的樹狀結(jié)果
**/
public static List<Node> parse(List<Node> nodeList) {
List<Node> resultList = new ArrayList<>(64);
nodeList.stream().filter(val -> !hasChild(nodeList, val.getMenuId())).collect(Collectors.toList())
.forEach(val -> {
reverseRecursion(val, nodeList, resultList);
});
Set<Node> set = new HashSet<>(32);
set.addAll(resultList);
resultList.clear();
set.stream().forEach(val -> {
resultList.add(val);
recursion(val, nodeList);
});
return resultList;
}
/**
* 取出某個(gè)節(jié)點(diǎn)直至末尾葉子節(jié)點(diǎn)的數(shù)據(jù)
*
* @author gewx
* @param node 葉子節(jié)點(diǎn)
* @param nodeList 數(shù)據(jù)節(jié)點(diǎn)集合
* @return 解析完成后的樹狀結(jié)果
**/
public static void getNodeJson(Node node, List<Node> nodeList) {
recursion(node, nodeList);
}
/**
* 倒序遞歸檢索所有父節(jié)點(diǎn),由下往上直到所有根節(jié)點(diǎn)
*
* @author gewx
* @param node 末尾葉子節(jié)點(diǎn)
* @param nodeList 數(shù)據(jù)節(jié)點(diǎn)集合
* @param resultList 根節(jié)點(diǎn)集合
* @return void
**/
private static void reverseRecursion(Node node, List<Node> nodeList, List<Node> resultList) {
List<Node> parentNodeList = nodeList.stream().filter(val -> val.getMenuId().equals(node.getParentId()))
.collect(Collectors.toList());
if (parentNodeList.size() != 0) {
Node parentNode = parentNodeList.get(0);
reverseRecursion(parentNode, nodeList, resultList);
} else {
resultList.add(node);
}
}
/**
* 遞歸檢索所有子節(jié)點(diǎn)
*
* @author gewx
* @param node 根節(jié)點(diǎn)
* @param nodeList 數(shù)據(jù)節(jié)點(diǎn)集合
* @return void
**/
private static void recursion(Node node, List<Node> nodeList) {
List<Node> childNodeList = nodeList.stream().filter(val -> val.getParentId().equals(node.getMenuId()))
.collect(Collectors.toList());
if (childNodeList.size() != 0) {
node.setChildren(childNodeList);
childNodeList.stream().forEach(val -> {
recursion(val, nodeList);
});
}
}
/**
* 是否還存在父級(jí)元素,找出末尾葉子節(jié)點(diǎn)
*
* @author gewx
*
* @param node 葉子節(jié)點(diǎn)
* @param menuId 節(jié)點(diǎn)標(biāo)記
* @return boolean true 存在, false 不存在
**/
private static boolean hasChild(List<Node> node, String menuId) {
return node.stream().anyMatch(val -> val.getParentId().equals(menuId));
}
/**
* 葉子節(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu),可與庫(kù)表做一一映射僅需要保持menuId與parentId映射關(guān)系即可
**/
@Getter
@Setter
@ToString
public static class Node {
/**
* 菜單Id
**/
private String menuId;
/**
* 菜單名稱
**/
private String menuName;
/**
* 菜單類型
**/
private String menuType;
/**
* 菜單編碼
**/
private String menuCode;
/**
* 菜單URL
**/
private String url;
/**
* 菜單父級(jí)Id
**/
public String parentId;
/**
* 排序
**/
private Integer sortNum;
/**
* 子菜單
**/
private List<Node> children = new ArrayList<>();
public Node(String menuId, String menuName, String menuType, String menuCode, String url, String parentId,
Integer sortNum) {
this.menuId = menuId;
this.menuName = menuName;
this.menuType = menuType;
this.menuCode = menuCode;
this.url = url;
this.parentId = parentId;
this.sortNum = sortNum;
}
@Override
public final boolean equals(Object obj) {
if (obj == null) {
return false;
}
Node otherObject = (Node) obj;
if (getClass() != otherObject.getClass()) {
return false;
}
EqualsBuilder builder = new EqualsBuilder();
builder.append(this.menuId, otherObject.getMenuId());
return builder.isEquals();
}
@Override
public final int hashCode() {
return this.menuId.hashCode() * 31;
}
}
public static void main(String[] args) {
List<Node> array = new ArrayList<>();
array.add(new Tree.Node("CODE_00", "用戶管理", "M", "0101", "#", "0", 0));
array.add(new Tree.Node("CODE_01", "管理界面", "M", "0102", "#", "0", 0));
array.add(new Tree.Node("CODE_02", "權(quán)限管理", "C", "010101", "http://www.baidu.com", "CODE_00", 0));
array.add(new Tree.Node("CODE_03", "內(nèi)部管理", "C", "010201", "http://www.baidu.com", "CODE_01", 0));
array.add(new Tree.Node("CODE_04", "內(nèi)部用戶權(quán)限", "C", "010201", "http://www.baidu.com", "CODE_02", 0));
array.add(new Tree.Node("CODE_05", "外部用戶權(quán)限", "C", "010201", "http://www.baidu.com", "CODE_02", 0));
array.add(new Tree.Node("CODE_06", "子權(quán)限", "C", "010201", "http://www.baidu.com", "CODE_04", 0));
array.add(new Tree.Node("CODE_07", "子系統(tǒng)內(nèi)部管理", "C", "010201", "http://www.baidu.com", "CODE_03", 0));
List<Node> list = Tree.parse(array);
String val = JSON.toJSONString(list);
System.out.println(val);
// 取出當(dāng)下節(jié)點(diǎn)下所有數(shù)據(jù)
Node node = new Tree.Node("CODE_01x", "系統(tǒng)管理員", "M", "0101", "#", "0", 0);
Tree.getNodeJson(node, array);
System.out.println(JSON.toJSONString(node));
}
}
解析結(jié)果:
[
{
"children": [
{
"children": [
{
"children": [],
"menuCode": "010201",
"menuId": "CODE_07",
"menuName": "子系統(tǒng)內(nèi)部管理",
"menuType": "C",
"parentId": "CODE_03",
"sortNum": 0,
"url": "http://www.baidu.com"
}
],
"menuCode": "010201",
"menuId": "CODE_03",
"menuName": "內(nèi)部管理",
"menuType": "C",
"parentId": "CODE_01",
"sortNum": 0,
"url": "http://www.baidu.com"
}
],
"menuCode": "0102",
"menuId": "CODE_01",
"menuName": "管理界面",
"menuType": "M",
"parentId": "0",
"sortNum": 0,
"url": "#"
},
{
"children": [
{
"children": [
{
"children": [
{
"children": [],
"menuCode": "010201",
"menuId": "CODE_06",
"menuName": "子權(quán)限",
"menuType": "C",
"parentId": "CODE_04",
"sortNum": 0,
"url": "http://www.baidu.com"
}
],
"menuCode": "010201",
"menuId": "CODE_04",
"menuName": "內(nèi)部用戶權(quán)限",
"menuType": "C",
"parentId": "CODE_02",
"sortNum": 0,
"url": "http://www.baidu.com"
},
{
"children": [],
"menuCode": "010201",
"menuId": "CODE_05",
"menuName": "外部用戶權(quán)限",
"menuType": "C",
"parentId": "CODE_02",
"sortNum": 0,
"url": "http://www.baidu.com"
}
],
"menuCode": "010101",
"menuId": "CODE_02",
"menuName": "權(quán)限管理",
"menuType": "C",
"parentId": "CODE_00",
"sortNum": 0,
"url": "http://www.baidu.com"
}
],
"menuCode": "0101",
"menuId": "CODE_00",
"menuName": "用戶管理",
"menuType": "M",
"parentId": "0",
"sortNum": 0,
"url": "#"
}
]
支持無限級(jí)聯(lián)樹,僅需要保證數(shù)據(jù)結(jié)構(gòu)中menuId與parentId需要存在級(jí)聯(lián)關(guān)系即可调炬。個(gè)人簡(jiǎn)單測(cè)試過语盈,小伙伴有需要可以再度驗(yàn)證下。