Java树形结构数据处理(递归处理)-可*更换业务实体-TreeUitls工具类详解
需求:
数据结构
id : 主键id
name : 显示名称
parentId : 父id
参考并借鉴 左轩大佬的这篇文章
https://blog.csdn.net/shinylp/article/details/78620592 (附上原链接,本文代码在基础上讲如何整合到自己的业务里面)
如何能添加到自己的业务里面,先看他原代码
import lombok.Data;
import java.util.List;
@Data
public class TreeNode {
/**
* 节点id
*/
private String id;
/**
* 父节点 默认0为根节点
*/
private String parentId;
/**
* 节点名称
*/
private String name;
/**
* 是否有子节点
*/
private boolean hasChild;
public TreeNode(String id, String parentId, String name) {
this.id = id;
this.parentId = parentId;
this.name = name;
}
========================工具类===============================
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 树形结构工具类
*
* 将一组list对象转成树形结构
* 该list需符合设定的字段类型
*
*/
public class TreeUtil {
public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
public List<TreeNode> menuCommon;
public List<Object> list = new ArrayList<Object>();
public List<Object> treeMenu(List<TreeNode> menu){
this.menuCommon = menu;
for (TreeNode treeNode : menu) {
Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
if(treeNode.getParentId().equals("0")){
setTreeMap(mapArr,treeNode);
list.add(mapArr);
}
}
return list;
}
public List<?> menuChild(String id){
List<Object> lists = new ArrayList<Object>();
for(TreeNode a:menuCommon){
Map<String,Object> childArray = new LinkedHashMap<String, Object>();
if(a.getParentId() .equals(id)){
setTreeMap(childArray,a);
lists.add(childArray);
}
}
return lists;
}
private void setTreeMap(Map<String,Object> mapArr,TreeNode treeNode){
mapArr.put("id", treeNode.getId());
mapArr.put("name", treeNode.getName());
mapArr.put("parentId", treeNode.getParentId());
List<?> childrens = menuChild(treeNode.getId());
if(childrens.size()>0){
mapArr.put("hasChild",true);
}
else{
mapArr.put("hasChildren",false);
}
mapArr.put("childrens", menuChild(treeNode.getId()));
}
public static void main(String[] args){
List<TreeNode> treeNodeList = new ArrayList<>();
TreeNode treeNode1 = new TreeNode("1","0","首页");
TreeNode treeNode2 = new TreeNode("2","0","订单");
TreeNode treeNode3 = new TreeNode("3","1","预约");
TreeNode treeNode4 = new TreeNode("4","2","捐献");
TreeNode treeNode5 = new TreeNode("5","4","我的订单");
TreeNode treeNode6 = new TreeNode("6","5","个人中心");
TreeNode treeNode7 = new TreeNode("7","6","个人中心2");
TreeNode treeNode8 = new TreeNode("8","99","个人中心3");
treeNodeList.add(treeNode1);
treeNodeList.add(treeNode6);
treeNodeList.add(treeNode5);
treeNodeList.add(treeNode3);
treeNodeList.add(treeNode4);
treeNodeList.add(treeNode2);
treeNodeList.add(treeNode7);
treeNodeList.add(treeNode8);
TreeUtil treeUtil = new TreeUtil();
System.out.print(JsonUtil.toJson(treeUtil.treeMenu(treeNodeList)));
}
}
最后得出了
这样的数据结构,那我们如何整合到自己的业务里面
我的数据结构
public class SFlatInfoVo {
@ApiModelProperty(value="主键ID",name="id",example="1")
private Integer id; // 主键
@ApiModelProperty(value="对应id",name="st_id",example="ST0001")
private String st_id; // 对应studio系统id
@ApiModelProperty(value="上级ID",name="parent_id",example="1")
private String parent_id; // 上级ID
@ApiModelProperty(value="空间名称",name="flat_name",example="一楼")
private String flat_name; // 名称
private String label; // elementUI需要label来显示,记得同步flat_name
List<SFlatInfoVo> children; //该楼层下的子楼层
}
我们把工具类放进去,并改造
public class TreeUtil {
public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
public List<SFlatInfoVo> menuCommon; //放你的结构
public List<Object> list = new ArrayList<>();
public List<Object> treeMenu(List<SFlatInfoVo> menu){
this.menuCommon = menu;
for (SFlatInfoVo treeNode : menu) {
Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
if(treeNode.getParent_id().equals("0")){ //这边我写的是更节点默认是0
setTreeMap(mapArr,treeNode);
list.add(mapArr);
}
}
return list;
}
public List<?> menuChild(String id){
List<Object> lists = new ArrayList<Object>();
for(SFlatInfoVo a:menuCommon){
Map<String,Object> childArray = new LinkedHashMap<String, Object>();
if(a.getParent_id().equals(id)){ //这里替换下父id
setTreeMap(childArray,a);
lists.add(childArray);
}
}
return lists;
}
//写上你的类的结构
private void setTreeMap(Map<String,Object> mapArr,SFlatInfoVo treeNode){
mapArr.put("st_id", treeNode.getSt_id());
mapArr.put("label", treeNode.getFlat_name());
mapArr.put("flat_name", treeNode.getFlat_name());
mapArr.put("parent_id", treeNode.getParent_id());
mapArr.put("children", menuChild(treeNode.getSt_id()));
}
}
工具类写好了,怎么用呢?
TreeUtil treeUtil = new TreeUtil();
List<Object> objects = treeUtil.treeMenu(allList);
String s = JSONUtil.toJsonString(objects);
List<SFlatInfoVo> infoVos = JSONUtil.parseArray(s, SFlatInfoVo.class);
这样就能获得树形结构数据了
最后附上json工具类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* JSON解析工具类
*
* @author
* @date
*/
public class JSONUtil {
private final static ObjectMapper objectMapper = new ObjectMapper();
/**
* 对象转json
* @param object
* @return
*/
public static String toJsonString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* json转换换成对象
*
* @param json
* @param clazz
* @return
*/
public static <T> T parseObject(String json, Class<T> clazz) {
T result = null;
try {
result = objectMapper.readValue(json, clazz);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* json转换换成集合
*
* @param json
* @param clazz
* @return
*/
public static <T> List<T> parseArray(String json, Class<T> clazz) {
List<T> result = new ArrayList<T>();
try {
List<Map<String, Object>> list = objectMapper.readValue(json, new TypeReference<List<T>>() {
});
for(Map<String, Object> one : list){
result.add(map2object(one, clazz));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static List<String> parseArray(String json) {
List<String> result = new ArrayList<String>();
try {
List<String> list = objectMapper.readValue(json, new TypeReference<List<String>>() {
});
result.addAll(list);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static <T> T map2object(Map<String, Object> map, Class<T> clazz) {
return objectMapper.convertValue(map, clazz);
}
}