用JAVA写无限级树形菜单代码
更新:HHH   时间:2023-1-7


这篇文章主要介绍“用JAVA写无限级树形菜单代码 ”,在日常操作中,相信很多人在用JAVA写无限级树形菜单代码 问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”用JAVA写无限级树形菜单代码 ”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

由于工作中经常碰见树形结构所写的一个公用方法,虽然之前有过无限级的代码不过都限制于对象,对象不同或对象中字段不同都无法使用。

此方法可以接受任意类型的List集合,返回时是已经拼接好了所有子集的List集合。注意方法接收的List合返回的List是同一个对象。

此方法采用的是Map形式实现,所以在参数方面需要提供字段名,这样就可以避免父级和自己字段不同从而多写很多重复的代码。另外子集的名称是可以自定义的。如果名字和我重构方法中相同可以用重构方法(其实这个没啥用就是可能方便一点)

自己写的大佬勿喷,如果有更好的方式可以评论,感谢大家!

private static List<Map<String,Object>> all;

/**
 *
 * @param list 任何类型的list集合
 * @param id 本层的id的字段名
 * @param parentId 上一层Id的字段名
 * @param childrenListName 对象中子集的名
 * @param firstId 最高层的父级Id
 * @return 返回任意类型List
 * @throws Exception 转换异常
 */
public static List<T> toJson(List<?> list, String id, String parentId, String childrenListName,Integer firstId) throws Exception {
    List<Map<String,Object>> mapList = new ArrayList<>();
    for(Object o : list){
        Map<String,Object> map = ObjectToMapUtils.objectToMap(o);
        mapList.add(map);
    }
    all = new ArrayList<>(mapList);
    List<Map<String,Object>> root = new ArrayList<>();
    for(Map<String,Object> map : mapList){
        if(Integer.parseInt(map.get(parentId).toString()) == firstId){
            root.add(map);
        }
    }
    all.removeAll(root);

    for(Map<String,Object> map : root){
        map.put(childrenListName,getChildren(map,id,parentId,childrenListName));
    }
    Gson gson = new Gson();
    List<T> lists = gson.fromJson(JSONObject.toJSONString(root),list.getClass());
    return lists;
}
public static List<T> toJson(List<?> list) throws Exception{
    return toJson(list,"id","parentId","list",-1);
}
public static List<T> toJson(List<?> list,String id) throws Exception{
    return toJson(list,id,"parentId","list",-1);
}
public static List<T> toJson(List<?> list,String id,String parentId) throws Exception{
    return toJson(list,id,parentId,"list",-1);
}
public static List<T> toJson(List<?> list,String id,String parentId,String childrenListName) throws Exception{
    return toJson(list,id,parentId,childrenListName,-1);
}

public static List<Map<String,Object>> getChildren(Map<String,Object> parent,String id,String parentId,String childrenListName){
    List<Map<String,Object>> mapList;
    if(parent.containsKey(childrenListName) && parent.get(childrenListName) != null){
        mapList = (List<Map<String, Object>>) parent.get(childrenListName);
    }else{
        mapList = new ArrayList<>();
    }
    for(Map<String,Object> map : all){
        if(Integer.parseInt(parent.get(id).toString()) == Integer.parseInt(map.get(parentId).toString())){
            mapList.add(map);
        }
    }

    if(mapList != null){
        all.removeAll(mapList);
        for(Map<String,Object> map : mapList){ 
            map.put(childrenListName,getChildren(map,id,parentId,childrenListName)); 
        }
    }
    return mapList;
}

public static void main(String[] arg){
    List<T> list = new ArrayList<>();
    try{
        TreeUtils.toJson(list,"id","parentId","list",0);
    }catch (Exception e){

    }
}

到此,关于“用JAVA写无限级树形菜单代码 ”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!

返回大数据教程...