通用层级型结构处理工具类,菜单树,部门树,地区树等..
2021/5/3 10:57:42
本文主要是介绍通用层级型结构处理工具类,菜单树,部门树,地区树等..,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/** * 列表型,层次结构 * @param list */ public static void createTreeTable(List<Test> list){ TreeMap<Long, List<Test>> collect = list.parallelStream().collect(Collectors.groupingBy(Test::getParentId, TreeMap::new, Collectors.toList())); System.out.println(collect); } /** * 有层级关系集合转,树形结构 ,适用用于 菜单树,部门树, 地区树 -->场景 * example:TreeUtil.createTree(null, all, "id", "parentId", "child", 0L); * null 为全遍历 * @param t 遍历对象, 当前父级 * @param list 原集合 * @param curFiled 当前本级标识字段属性 eg :id * @param parentFiled 当前父级标识字段属性 eg :parentId * @param chidrenFiled 子集合存储标识属性 eg :child * @param topTag 顶级标识字段属性 * @param <T> 泛型 * @return 展开型 list,传入对象的所有下级,不包含当前,如要包含 t.setChild(TreeUtil.createTree(null, all, "id", "parentId", "child", 0L)) */ public static <T> List<T> createTree(T t,List<T> list,String curFiled,String parentFiled,String chidrenFiled,Object topTag){ List<T> children; if (t != null) { children = list.parallelStream().filter(e -> Objects.equals(getInvoke(parentFiled, e), getInvoke(curFiled, t))).collect(Collectors.toList()); } else { children = list.parallelStream().filter(e -> getInvoke(parentFiled,e) == topTag).collect(Collectors.toList()); } if (children.size() == 0) { return null; } children.forEach(child -> setInvoke(chidrenFiled,child,createTree(child, list, curFiled, parentFiled, chidrenFiled, topTag))); return children; } /** * 根据属性名获取属性值 get * @param fieldName 属性名称 * @param o 调用对象 * @return 返回值 */ private static Object getInvoke(String fieldName, Object o) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = String.format("get%s%s", firstLetter,fieldName.substring(1)); Method method = o.getClass().getDeclaredMethod(getter); return method.invoke(o); } catch (Exception e) { System.out.println(fieldName+">>>>方法执行异常"); return null; } } /** * 根据属性名获取属性值 set * @param fieldName 属性名称 * @param o 调用对象 * @return 返回值 */ private static void setInvoke(String fieldName, Object o,List arg) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String setter = String.format("set%s%s", firstLetter,fieldName.substring(1)); Method method = o.getClass().getDeclaredMethod(setter,List.class); method.invoke(o,arg); } catch (Exception e) { System.out.println(fieldName+">>>>方法执行异常"); } }
这篇关于通用层级型结构处理工具类,菜单树,部门树,地区树等..的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)