获取数据生成树形数据后,要求返回返回层级(level)和子集(childCount)的个数,便于前端处理。
1,在遍历的时候,进行处理。 (适合内部调用)
2,在生产树形后,再遍历处理。(适合外部调用,获取的数据已经是树形了,不好改引用的接口)
import java.util.ArrayList;
import java.util.List;
public class TreeModel {
private String name;
private String code;
private String parentCode;
private int level;
private int childCount;
private List children;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
public List getChildren() {
return children;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getChildCount() {
return childCount;
}
public void setChildCount(int childCount) {
this.childCount = childCount;
}
public void setChildren(List children) {
if (children.size() == 0) {
this.children = new ArrayList<>();
} else {
this.children = children;
}
}
@Override
public String toString() {
return "{"
+ "\"name\":\""
+ name + '\"'
+ ",\"code\":\""
+ code + '\"'
+ ",\"parentCode\":\""
+ parentCode + '\"'
+ ",\"level\":"
+ level
+ ",\"childCount\":"
+ childCount
+ ",\"children\":"
+ children
+ "}";
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yan.web.util.treeNode.listMap1.TreeModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
public class TreeModelWithLevel {
public static void main(String[] args){
List areaList = initData();
// 构建树形
List treeModels = parseTreeModel(areaList);
System.out.println("result: "+ JSON.toJSONString(treeModels));
}
private static List parseTreeModel(List treeModels) {
if (CollectionUtils.isEmpty(treeModels)) {
return Collections.emptyList();
}
Map> map = new HashMap<>();
treeModels.forEach(x -> {
String parentCode = x.getParentCode();
if (StringUtils.isBlank(parentCode)) {
parentCode = "-1";
}
// 如果 parentCode 没有值,生成空数组,
List list = map.computeIfAbsent(parentCode, k -> new ArrayList<>());
// 如果有值,就把值取出来,相同的parentCode 放到一起
list.add(x);
});
// 从-1开始遍历
List result = map.getOrDefault("-1", new ArrayList<>());
parseChildrenTreeModel(0, map, result);
return result;
}
private static void parseChildrenTreeModel(int level, Map> map, List treeModels) {
level++;
for (TreeModel x : treeModels) {
List orDefault = map.getOrDefault(x.getCode(), new ArrayList<>());
x.setChildren(orDefault);
x.setLevel(level);
x.setChildCount(orDefault.size());
if (!CollectionUtils.isEmpty(x.getChildren())) {
parseChildrenTreeModel(level, map, x.getChildren());
}
}
}
private static List initData(){
String areaInfo = "[{\n" +
"\t\t\"code\": 59,\n" +
"\t\t\"parentCode\": -1,\n" +
"\t\t\"name\": \"福建\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 591,\n" +
"\t\t\"parentCode\": 59,\n" +
"\t\t\"name\": \"福州\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59101,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"鼓楼区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59195,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"晋安区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59196,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"马尾区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59197,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"台江区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59198,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"金山区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 592,\n" +
"\t\t\"parentCode\": 59,\n" +
"\t\t\"name\": \"厦门\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59201,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"思明区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59202,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"同安\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59203,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"杏林\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59204,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"海沧区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59205,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"同安区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59206,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"翔安区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 593,\n" +
"\t\t\"parentCode\": 59,\n" +
"\t\t\"name\": \"宁德\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59301,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"宁德\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59302,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"古田\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59303,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"屏南\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59304,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"寿宁\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59305,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"周宁\"\n" +
"\t}]";
JSONArray areaArr = JSONArray.parseArray(areaInfo);
return ListUtils.emptyIfNull(areaArr).stream().map(e -> (JSONObject) e)
.map(e -> JSONObject.parseObject(e.toString(), TreeModel.class)).collect(Collectors.toList());
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yan.web.util.treeNode.listMap1.TreeModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
public class TreeModelAddLevel {
public static void main(String[] args){
List areaList = initData();
// 构建树形
List treeModels = parseTreeModel(areaList);
System.out.println("result: "+ JSON.toJSONString(treeModels));
addLevel(0, treeModels);
System.out.println("result2: "+ JSON.toJSONString(treeModels));
}
private static void addLevel(int level, List treeModels){
level++;
for (TreeModel treeModel : ListUtils.emptyIfNull(treeModels)) {
treeModel.setLevel(level);
List children = treeModel.getChildren();
treeModel.setChildCount(children.size());
if(CollectionUtils.isNotEmpty(children)){
addLevel(level, children);
}
}
}
private static List parseTreeModel(List treeModels) {
if (CollectionUtils.isEmpty(treeModels)) {
return Collections.emptyList();
}
Map> map = new HashMap<>();
treeModels.forEach(x -> {
String parentCode = x.getParentCode();
if (StringUtils.isBlank(parentCode)) {
parentCode = "-1";
}
// 如果 parentCode 没有值,生成空数组,
List list = map.computeIfAbsent(parentCode, k -> new ArrayList<>());
// 如果有值,就把值取出来,相同的parentCode 放到一起
list.add(x);
});
// 从-1开始遍历
List result = map.getOrDefault("-1", new ArrayList<>());
parseChildrenTreeModel(map, result);
return result;
}
private static void parseChildrenTreeModel(Map> map, List treeModels) {
treeModels.forEach(x -> {
x.setChildren(map.getOrDefault(x.getCode(), new ArrayList<>()));
if (!CollectionUtils.isEmpty(x.getChildren())) {
parseChildrenTreeModel(map, x.getChildren());
}
});
}
private static List initData(){
String areaInfo = "[{\n" +
"\t\t\"code\": 59,\n" +
"\t\t\"parentCode\": -1,\n" +
"\t\t\"name\": \"福建\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 591,\n" +
"\t\t\"parentCode\": 59,\n" +
"\t\t\"name\": \"福州\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59101,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"鼓楼区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59195,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"晋安区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59196,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"马尾区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59197,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"台江区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59198,\n" +
"\t\t\"parentCode\": 591,\n" +
"\t\t\"name\": \"金山区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 592,\n" +
"\t\t\"parentCode\": 59,\n" +
"\t\t\"name\": \"厦门\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59201,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"思明区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59202,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"同安\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59203,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"杏林\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59204,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"海沧区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59205,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"同安区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59206,\n" +
"\t\t\"parentCode\": 592,\n" +
"\t\t\"name\": \"翔安区\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 593,\n" +
"\t\t\"parentCode\": 59,\n" +
"\t\t\"name\": \"宁德\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59301,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"宁德\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59302,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"古田\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59303,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"屏南\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59304,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"寿宁\"\n" +
"\t},\n" +
"\t{\n" +
"\t\t\"code\": 59305,\n" +
"\t\t\"parentCode\": 593,\n" +
"\t\t\"name\": \"周宁\"\n" +
"\t}]";
JSONArray areaArr = JSONArray.parseArray(areaInfo);
return ListUtils.emptyIfNull(areaArr).stream().map(e -> (JSONObject) e)
.map(e -> JSONObject.parseObject(e.toString(), TreeModel.class)).collect(Collectors.toList());
}
}
在生成树形数据的时候,要添加层级和子集个数,可以
1,在遍历的时候,进行处理。 (适合内部调用)
2,在生产树形后,再遍历处理。(适合外部调用,获取的数据已经是树形了,不好改引用的接口)