JavaList与树的互转

JavaList与树的互转

2023年6月22日发(作者:)

JavaList与树的互转  平时⼯作中都会遇到包含层级关系的List数据转换成树形结构,或者数据已是树形结构了,需要我们处理成普通的单层list结构。以下代码均为本⼈实际开发所写代码,可能不是最优解、复杂度也⽐较⾼,在此和⼤家⼀起分享学习!  注:该⼯具类⽀持将list转换成树/森林。可⾃⾏测试,有疑问或更优⽅案,可私聊我。

  TreeNode@Data@JsonInclude(_NULL)public class TreeNode { private String id; private String key; private String value;   private String name; private String parentId; private String createName; private LocalDateTime createTime; private List> children; private boolean hasParent = false; private boolean hasChildren = false; public void initChildren() { en = new ArrayList<>(); }}TreeUtilpublic class TreeUtil { protected TreeUtil() { } private final static String TOP_NODE_ID = "0"; /** * 使⽤递归⽅法建树 * * @param nodes * @return */ public static List> buildByRecursive(List> nodes) { List> trees = new ArrayList<>(); for (TreeNode treeNode : nodes) { if (TOP_NODE_(entId())) { (findChildren(treeNode,nodes)); } } return trees; } /** * 递归查找⼦节点 * * @param treeNodes * @return */ public static TreeNode findChildren(TreeNode treeNode, List> treeNodes) { for (TreeNode it : treeNodes) { if (().equals(entId())) { if (ldren() == null) { ldren(new ArrayList>()); } ldren().add(findChildren(it, treeNodes)); } } return treeNode; } public static void buildTrees(List> trees, List list) { h(entity -> { TreeNode tree = new TreeNode<>(); (()); (()); ue(()); e(e()); entId(entId()); ateTime(ateTime()); ateName(ateName()); (tree); }); } public static List> buildTreeList(List> nodes) { if (nodes == null) { return null; } List> topNodes = new ArrayList<>(); //利⽤两层循环,组装树/森林结构 h(first -> { String pid = entId(); Parent(true); if (pid == null || TOP_NODE_(pid)) { (first); Parent(false); } else { boolean isExistPid = false; for (TreeNode temp : nodes) { if (().equals(pid)) { isExistPid = true; } } if (!isExistPid) { (first); Parent(false); } } for (TreeNode second : nodes) { String id = (); if (id != null && (pid)) { if (ldren() == null) { ildren(); } ldren().add(first); Parent(true); Children(true); Parent(true); return; } } }); return topNodes; } public static TreeNode buildTree(List> nodes) { if (nodes == null) { return null; } List> topNodes = new ArrayList<>(); //利⽤两层循环,组装树/森林结构 h(first -> { String pid = entId(); Parent(true); if (pid == null || TOP_NODE_(pid)) { (first); Parent(false); } else { boolean isExistPid = false; for (TreeNode temp : nodes) { if (().equals(pid)) { isExistPid = true; } } if (!isExistPid) { (first); Parent(false); } } for (TreeNode second : nodes) { String id = (); if (id != null && (pid)) { if (ldren() == null) { ildren(); } ldren().add(first); Parent(true); Children(true); Parent(true); return; } } }); TreeNode root = new TreeNode<>(); ("0"); entId(""); Parent(false); Children(true); ldren(topNodes); e("root"); return root; } public static List> tree2List(List> treeList) { List> list = new ArrayList<>(); for (TreeNode tree : treeList) { List> child = ldren(); (tree); if (child != null && () > 0) { (tree2List(child)); ldren(null); } } return list; }}

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687385645a6150.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信