重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

用JAVA写无限级树形菜单代码

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

创新互联公司网站建设服务商,为中小企业提供成都网站设计、网站制作服务,网站设计,网站托管维护等一站式综合服务型公司,专业打造企业形象网站,让您在众多竞争对手中脱颖而出创新互联公司

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

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

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

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

private static List> all;

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

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

public static List> getChildren(Map parent,String id,String parentId,String childrenListName){
    List> mapList;
    if(parent.containsKey(childrenListName) && parent.get(childrenListName) != null){
        mapList = (List>) parent.get(childrenListName);
    }else{
        mapList = new ArrayList<>();
    }
    for(Map 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 map : mapList){ 
            map.put(childrenListName,getChildren(map,id,parentId,childrenListName)); 
        }
    }
    return mapList;
}

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

    }
}

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


网页名称:用JAVA写无限级树形菜单代码
URL标题:http://cqcxhl.com/article/giojgj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP