/**
* 节点实体类
* @author Administrator
*
*/
public class Nodes {
private String id;
//父节点
private String pid;
private String name;
List<Nodes> childNodes;
Nodes(String id,String pid,String name){
this.id = id;
this.pid = pid;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Nodes> getChildNodes() {
return childNodes;
}
public void setChildNodes(List<Nodes> childNodes) {
this.childNodes = childNodes;
}
}
/**
* 树状结构生生成
* @author Administrator
*
*/
public class NodesUtils {
//判断是否根节点,可根据实际情况修改
public static boolean isRootElement(Nodes node){
if(node.getPid().equals("0")){
return true;
}
return false;
}
/**
* 获取子节点
* 递归获取子节点的子节点
* @param pid
* @param rootList
* @return
*/
public static List<Nodes> getChildNodes(String pid,List<Nodes> rootList){
List<Nodes> childList = new ArrayList<>();
for(Nodes n:rootList){
if(n.getPid().equals(pid)){
childList.add(n);
}
}
//递归查找子节点的子节点并赋值
for(Nodes c:childList){
c.setChildNodes(getChildNodes(c.getId(), rootList));
}
//子节点查找结束
if(childList.size() == 0){
return null;
}
return childList;
}
public static void main(String[] args) {
List<Nodes> nodesList = new ArrayList<>();
nodesList.add(new Nodes("1", "0", "1"));
nodesList.add(new Nodes("1.1", "1", "1.1"));
nodesList.add(new Nodes("1.2", "1", "1.2"));
nodesList.add(new Nodes("1.2.1", "1.2", "1.2.1"));
nodesList.add(new Nodes("1.2.1.1", "1.2.1", "1.2.1.1"));
nodesList.add(new Nodes("1.3", "1", "1.3"));
nodesList.add(new Nodes("1.4", "1", "1.4"));
nodesList.add(new Nodes("1.3.1", "1.3", "1.3.1"));
nodesList.add(new Nodes("1.4.1", "1.4", "1.4.1"));
nodesList.add(new Nodes("1.4.1.1", "1.4。1", "1.4.1.1"));
List<Nodes> rootlist = new ArrayList<>();
//查找所有根节点
for (Nodes n:nodesList) {
if (isRootElement(n)) {
rootlist.add(n);
}
}
//根据根节点查找所有子节点
for(Nodes nc:rootlist){
nc.setChildNodes(getChildNodes(nc.getId(), nodesList));
}
//输出
buildMenu(rootlist);
System.out.println(str);
}
/**
* 遍历菜单
* @param arr
* @return
*/
public static String str = "";
public static String buildMenu(List<Nodes> arr){
for(int i=0;i<arr.size();i++){
str += "<li>";
str += "<a href='javascript:;'>" +
"<cite>"+arr.get(i).getId()+"</cite>" +
"<i class='iconfont nav_right'> "+arr.get(i).getName()+"</i>" +
"</a>";
//存在子菜单 递归
if(arr.get(i).getChildNodes() != null && arr.get(i).getChildNodes().size() > 0){
str += "<ul>";
buildMenu(arr.get(i).getChildNodes()); // 递归
str += "</ul>";
}
str += "</li>";
}
return str;
}
}