处理查询到的数据是List<Map<String,Object>格式
方法 一:处理List<Map<String,Object>类型的数据
注意:方法一中的数据根节点的数据必须放在list集合的首位
//测试可实现
public class test {
public static void main(String[] args) {
List<Map<String, Object>> l = new ArrayList();
Map<String, Object> m = new HashMap();
m.put("id", "1");
m.put("name", "根节点");
m.put("pid", "0");
Map<String, Object> m1 = new HashMap();
m1.put("id", "11");
m1.put("name", "根节点");
m1.put("pid", "1");
Map<String, Object> m2 = new HashMap();
m2.put("id", "12");
m2.put("name", "根节点");
m2.put("pid", "11");
l.add(m);
l.add(m1);
l.add(m2);
System.out.println(getDepartmentTreeList(l).toString());
}
private static List getDepartmentTreeList(List<Map<String, Object>> l) {
List ldata = new ArrayList();
Map<String, Object> mjl = new HashMap<>();
for (Map m : l) {
String id = m.get("id").toString();
String parent_id = m.get("pid").toString();
//if ("根节点id".equals(id)) 此处的根节点id为1
if ("1".equals(id)) {
ldata.add(m);
}
///如果包含id 就说明已经自动创建了需要属性覆盖
if (mjl.containsKey(id)) {
Map m1 = (Map) mjl.get(id);
for (Object entry : m.entrySet()) {
Map.Entry e = (Map.Entry) entry;
m1.put(e.getKey(), e.getValue());
}
} else {
mjl.put(id, m);
}
List lmp = null;
if (!ObjectUtil.isNull(mjl.get(parent_id))) {
Map m2 = (Map) mjl.get(parent_id);
if (ObjectUtil.isNull(m2.get("children"))) {
m2.put("children", new ArrayList<>());
}
lmp = (List) m2.get("children");
} else {
lmp = new ArrayList();
Map m1 = new HashMap();
m1.put("id", parent_id);
m1.put("children", lmp);
mjl.put(parent_id, m1);
}
lmp.add(mjl.get(id));
}
return ldata;
}
输出:
[{children=[{children=[{name=根节点, pid=11, id=12}], name=根节点, pid=1, id=11}], name=根节点, pid=0, id=1}]
方法二:处理List<Map<String,Object>类型的数据
方法二是看的网上大神处理List<对象>类型的数据的例子,针对List<Map<String,Object>类型数据做的修改。
- getTreeDataUtils是一个帮助类,直接拿过去就可以用。需要修改一下里面的code和pcode,我这里的code和pcode就是id和parentid.
public class getTreeDataUtils {
private List<Map<String,Object>> rootList; //根节点对象存放到这里
private List<Map<String,Object>> bodyList; //其他节点存放到这里,可以包含根节点
//有参构造器
public getTreeDataUtils(List<Map<String,Object>> rootList, List<Map<String,Object>> bodyList) {
this.rootList = rootList;
this.bodyList = bodyList;
}
public List<Map<String,Object>> getTree(){ //调用的方法入口
if(bodyList != null && !bodyList.isEmpty()){
//声明一个map,用来过滤已操作过的数据
Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
rootList.forEach( m -> getChild(m,map));
return rootList;
}
return null;
}
public void getChild(Map<String,Object> m,Map<String,String> map){
// Map<String,Object> m2=new HashMap<>();
List<Map<String,Object>> setChildTreeDto=new ArrayList<>();
List<Map<String,Object>> childList = Lists.newArrayList();
//此处的code就是id,pcode就是parentid 可根据自己的具体的字段替换
bodyList.stream()
.filter(c -> !map.containsKey(c.get("code").toString()))
.filter(c ->c.get("pcode").toString().equals(m.get("code").toString()))
.forEach(c ->{
map.put(c.get("code").toString(),c.get("pcode").toString());
getChild(c,map);
childList.add(c);
});
// m.setChildTreeDto(childList);
List lmp = null;
m.put("children", childList);
}
}
方法二调用的方式:
// 此处要有根节点的数据集合rootList和节点数据集合bodyList两个集合,将你的两个集合传入getTreeDataUtils(rootList,bodyList)即可。
getTreeDataUtils utils = new getTreeDataUtils(rootList,bodyList);
List<Map<String,Object>> result = utils.getTree();
//result 就是你想要的结果
方法三:处理List<对象>类型的数据
这个方法是从网上看到的,
这个实现方式就是方法二中说的网上看的例子,
点击查看方法三