这篇文章主要介绍“用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写无限级树形菜单代码 ”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3037265/blog/3125736