网上找了很多文章,很少看到有递归工具类,今天给大家写一个返回tree的json工具类,简单好用,用了jdk 8.0的方法,需要用的朋友记得安装jdk 8.0,若有更好的工具类,不吝赐教。
数据库 注意字段city_code、city_name
工具类
/**
/**
* 生成树形json
* @param list 传入的数据源
* @param DepartmentList 接收的数据源
* @param city_code 字段名
* @param city_name字段名
*/
public void getTreeJson(List<LinkedHashMap<String,Object>> list,List<LinkedHashMap<String,Object>> DepartmentList,
String city_code,String city_name) {
if(list!=null) {
List<LinkedHashMap<String,Object>> filterList = list.stream().filter(a -> a.get("pid")==null).collect(Collectors.toList());
for (LinkedHashMap<String, Object> linkedHashMap : filterList) {
LinkedHashMap<String,Object> HashMap=new LinkedHashMap<String,Object>();
String pid = linkedHashMap.get("pid")==null?null:linkedHashMap.get("pid").toString();
if(StringUtils.isEmpty(pid)) {
String departmentId=linkedHashMap.get(city_code).toString();
HashMap.put(city_code, departmentId);
HashMap.put(city_name, linkedHashMap.get(city_name));
List<LinkedHashMap<String,Object>> childrenList = list.stream().filter(a -> a.get("pid")!=null&&a.get("pid").toString().equals(departmentId)).collect(Collectors.toList());
List<LinkedHashMap<String,Object>> childrenJson = getChildren(list,childrenList,city_code,city_name);
HashMap.put("children", childrenJson);
DepartmentList.add(HashMap);
}
}
}
}
private List<LinkedHashMap<String,Object>> getChildren(List<LinkedHashMap<String,Object>> list,List<LinkedHashMap<String,Object>> childrenList,
String city_code,String city_name){
List<LinkedHashMap<String,Object>> List=new ArrayList<LinkedHashMap<String,Object>>();
for (LinkedHashMap<String, Object> linkedHashMap : childrenList) {
LinkedHashMap<String,Object> HashMap=new LinkedHashMap<String,Object>();
String departmentId=linkedHashMap.get(city_code).toString();
HashMap.put(city_code, departmentId);
HashMap.put(city_name, linkedHashMap.get(city_name));
List<LinkedHashMap<String,Object>> childList = list.stream().filter(a -> a.get("pid")!=null&&a.get("pid").toString().equals(departmentId)).collect(Collectors.toList());
if(childList!=null&&childList.size()>0) {
List<LinkedHashMap<String,Object>> drenlist = getChildren(list,childList,city_code,city_name);
HashMap.put("children",drenlist);
}
List.add(HashMap);
}
return List;
}
Service层调用
@Override
public List<LinkedHashMap<String,Object>> getAdministrativeDivision() {
List<LinkedHashMap<String,Object>> list = departmentMapper.getAdministrativeDivision();
List<LinkedHashMap<String,Object>> DepartmentList =new ArrayList<LinkedHashMap<String,Object>>();
if(list.size()>1) {
userActionLog.getTreeJson(list,DepartmentList,"city_code","city_name");
list=DepartmentList;
}
return list;
}
返回json
{
"status": 200,
"msg": "success",
"data": {
"list": [{
"city_code": "310000",
"city_name": "上海市",
"children": [{
"city_code": "310100",
"city_name": "市辖区",
"children": [{
"city_code": "310101",
"city_name": "黄浦区"
}, {
"city_code": "310104",
"city_name": "徐汇区"
}, {
"city_code": "310105",
"city_name": "长宁区"
}, {
"city_code": "310106",
"city_name": "静安区"
}, {
"city_code": "310107",
"city_name": "普陀区"
}, {
"city_code": "310109",
"city_name": "虹口区"
}, {
"city_code": "310110",
"city_name": "杨浦区"
}, {
"city_code": "310112",
"city_name": "闵行区"
}, {
"city_code": "310113",
"city_name": "宝山区"
}, {
"city_code": "310114",
"city_name": "嘉定区"
}, {
"city_code": "310115",
"city_name": "浦东新区"
}, {
"city_code": "310116",
"city_name": "金山区"
}, {
"city_code": "310117",
"city_name": "松江区"
}, {
"city_code": "310118",
"city_name": "青浦区"
}, {
"city_code": "310120",
"city_name": "奉贤区"
}]
}]
}]
}
}