Java实现无限极分类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 递归
*/
public static List<Object> getTree(List<Menus> list, String pk, String pid, String child, int root) {
List<Object> tree = new ArrayList<Object>();

for (Menus menus : list) {
int menus_pid = menus.getpId();
if (menus_pid == root) {
int id = menus.getId();
MenusCustom menusCustom = new MenusCustom();
menusCustom.setId(menus.getId());
menusCustom.setName(menus.getName());
menusCustom.setpId(menus.getpId());
menusCustom.setUri(menus.getUri());
menusCustom.setChild(MenusUtil.getTree(list, pk, pid, child, id));
tree.add(menusCustom);
}
}
return tree;
}

// 利用重载设置默认参数
public static List<Object> getTree(List<Menus> list) {
return MenusUtil.getTree(list, "id", "pid", "child", 0);
}

这是 Menus 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Menus {
private Integer id;

private String name;

private String uri;

private Integer pId;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri == null ? null : uri.trim();
}

public Integer getpId() {
return pId;
}

public void setpId(Integer pId) {
this.pId = pId;
}
}

这是 MenusCustom 类

1
2
3
4
5
6
7
8
9
10
11
12
public class MenusCustom extends Menus {
public Object getChild() {
return child;
}

public void setChild(Object child) {
this.child = child;
}

private Object child;

}
LeoQin wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
0%