Java集合List转树结构工具类[通俗易懂]

Java集合List转树结构工具类[通俗易懂]业务场景:菜单树、组织架构树…..前端要求数据结构为树结构,而后端查出来的是一条一条的数据集,每次都要各种递归遍历很麻烦,特此写了一个工具类来解决.三个注解:importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;/***@a

大家好,又见面了,我是你们的朋友全栈君。

此版本太累赘,请转到函数版:https://blog.csdn.net/wenxingchen/article/details/115749782?spm=1001.2014.3001.5501

业务场景:菜单树、组织架构树…..前端要求数据结构为树结构,而后端查出来的是一条一条的数据集,每次都要各种递归遍历很麻烦,特此写了一个工具类来解决.

  • 三个注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author sunziwen
 * @since 2021-4-13 16:19:05
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeId {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author sunziwen
 * @since 2021-4-13 16:19:05
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeParentId {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author sunziwen
 * @since 2021-4-13 16:19:05
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeChildren {
}

一个工具类:

 

import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 树形工具类
 *
 * @author sunziwen
 * @since 2021-4-13 16:19:05
 */

public class TreeUtil {
    /**
     * 找出顶层节点
     *
     * @return data
     */
    @SneakyThrows
    private <T> List<T> treeOut(List<T> list) {
        //数据不能为空
        if (list == null || list.size() <= 0) {
            return list;
        }
        //获取泛型T的class
        Class<?> aClass = list.get(0).getClass();

        Field[] declaredFields = aClass.getDeclaredFields();
        //获取主键属性
        List<Field> idPropertyField = Arrays.stream(declaredFields).filter(x -> {
            TreeId annotation = x.getAnnotation(TreeId.class);
            return annotation != null;
        }).collect(Collectors.toList());
        if (idPropertyField.size() <= 0) {
            throw new RuntimeException("缺失@TreeId注解");
        }
        if (idPropertyField.size() > 1) {
            throw new RuntimeException("@TreeId注解只能存在一个");
        }
        //获取父节点属性
        List<Field> parentIdPropertyField = Arrays.stream(declaredFields).filter(x -> {
            TreeParentId annotation = x.getAnnotation(TreeParentId.class);
            return annotation != null;
        }).collect(Collectors.toList());
        if (parentIdPropertyField.size() <= 0) {
            throw new RuntimeException("缺失@ParentId注解");
        }
        if (parentIdPropertyField.size() > 1) {
            throw new RuntimeException("@ParentId注解只能存在一个");
        }

        /*主键的属性名*/
        String idPropertyName = idPropertyField.get(0).getName();
        /*主键的get方法*/
        Method getId = aClass.getMethod("get" + StrUtil.upperFirst(idPropertyName));

        /*父节点的属性名*/
        String parentIdPropertyName = parentIdPropertyField.get(0).getName();
        /*父节点的get方法*/
        Method getParentId = aClass.getMethod("get" + StrUtil.upperFirst(parentIdPropertyName));

        /*所有元素的Id*/
        List<Object> ids = list.stream().map(x -> {
            try {
                return getId.invoke(x);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
        /*查出所有顶级节点*/
        List<T> topLevel = list.stream().filter(x -> {
            try {
                return !ids.contains(getParentId.invoke(x));
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            return false;
        }).collect(Collectors.toList());

        return recursion(topLevel, list);
    }


    /**
     * 递归装载
     *
     * @param superLevel 上级节点
     * @param list       数据集
     * @return
     */
    @SneakyThrows
    private <T> List<T> recursion(List<T> superLevel, List<T> list) {
        //获取泛型T的class
        Class<?> aClass = list.get(0).getClass();

        Field[] declaredFields = aClass.getDeclaredFields();
        //获取主键属性
        List<Field> idPropertyField = Arrays.stream(declaredFields).filter(x -> {
            TreeId annotation = x.getAnnotation(TreeId.class);
            return annotation != null;
        }).collect(Collectors.toList());
        if (idPropertyField.size() <= 0) {
            throw new RuntimeException("缺失@TreeId注解");
        }
        if (idPropertyField.size() > 1) {
            throw new RuntimeException("@TreeId注解只能存在一个");
        }
        //获取父节点属性
        List<Field> parentIdPropertyField = Arrays.stream(declaredFields).filter(x -> {
            TreeParentId annotation = x.getAnnotation(TreeParentId.class);
            return annotation != null;
        }).collect(Collectors.toList());
        if (parentIdPropertyField.size() <= 0) {
            throw new RuntimeException("缺失@ParentId注解");
        }
        if (parentIdPropertyField.size() > 1) {
            throw new RuntimeException("@ParentId注解只能存在一个");
        }

        //获取父节点属性
        List<Field> childrenPropertyField = Arrays.stream(declaredFields).filter(x -> {
            TreeChildren annotation = x.getAnnotation(TreeChildren.class);
            return annotation != null;
        }).collect(Collectors.toList());
        if (childrenPropertyField.size() <= 0) {
            throw new RuntimeException("缺失@TreeChildren注解");
        }
        if (childrenPropertyField.size() > 1) {
            throw new RuntimeException("@TreeChildren注解只能存在一个");
        }

        /*主键的属性名*/
        String idPropertyName = idPropertyField.get(0).getName();
        /*主键的get方法*/
        Method getId = aClass.getMethod("get" + StrUtil.upperFirst(idPropertyName));

        /*父节点的属性名*/
        String parentIdPropertyName = parentIdPropertyField.get(0).getName();
        /*父节点的get方法*/
        Method getParentId = aClass.getMethod("get" + StrUtil.upperFirst(parentIdPropertyName));

        /*子节点的属性名*/
        String childrenPropertyName = childrenPropertyField.get(0).getName();
        /*字节点的set方法*/
        Method setChildren = aClass.getMethod("set" + StrUtil.upperFirst(childrenPropertyName));


        for (T t : superLevel) {
            List<T> children = list.stream().filter(x -> {
                try {
                    return getParentId.invoke(x).equals(getId.invoke(t));
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
                return false;
            }).collect(Collectors.toList());
            if (children.size() <= 0) {
                continue;
            }
            List<T> recursion = recursion(children, list);
            setChildren.invoke(t, recursion);
        }
        return superLevel;
    }
}
  • 使用示例:
  • 
    import lombok.Data;
    
    import java.util.List;
    
    @Data
    public class My {
        @TreeId//在实体类的主键上打上该注解
        private String id;
    
        @TreeParentId//在实体类的父节点id上打上该注解
        private String parentId;
    
        private String name;
    
        @TreeChildren //在子集上打上该注解
        //@TableField(exist = false)//如果你用的是mybatis-plus则需要让框架忽略该字段
        private List<My> children;
    
        public My(String id, String parentId, String name) {
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }
    }
        public static void main(String[] args) {
            ArrayList<My> mies = new ArrayList<>();
            mies.add(new My("1", "-1", "a"));
            mies.add(new My("2", "-1", "aa"));
            mies.add(new My("3", "1", "b"));
            mies.add(new My("4", "1", "c"));
            mies.add(new My("5", "3", "d"));
            mies.add(new My("6", "5", "e"));
            mies.add(new My("7", "6", "f"));
            mies.add(new My("8", "2", "g"));
            mies.add(new My("9", "8", "h"));
            mies.add(new My("10", "9", "i"));
            List<My> mies1 = TreeUtil.treeOut(mies);
            System.out.println(mies1);
        }

    大功告成了,如果有问题请加博主V:sunziwen3366

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160344.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 声源定位系统设计(一)——MVDR波束形成算法「建议收藏」

    声源定位系统设计(一)——MVDR波束形成算法「建议收藏」本篇博客希望通过通俗的方式讲清一些简单经典声源定位的方法,并能够按照这种方法自制一个声源定位系统。

    2022年9月22日
    2
  • 【linux命令】 tree命令

    【linux命令】 tree命令文章目录Tree命令安装方法一,yum安装方法二,源码安装Tree命令安装方法一,yum安装命令:yuminstalltree方法二,源码安装1.下载安装包,地址:http://mama.indstate.edu/users/ice/tree/2.解压安装1)Linux环境(CentOS6.5)下安装a.解压tree-1.7.0.tgz文件,命令:tar-zxvftree-1.7.0.tgzb.进入解压目录中,命令:cdtree-1.7.0      c.安装文件,命令:

    2022年7月24日
    8
  • 【转载】COM文件与EXE文件的区别与联系

    【转载】COM文件与EXE文件的区别与联系

    2021年11月18日
    37
  • 编写java判断闰年_Java 判断闰年代码实例

    编写java判断闰年_Java 判断闰年代码实例importjava.util.Scanner;/*5.1判断闰年(时间:20分钟)5.1.1作业任务(1)由用户输入任意一个年份,能被4整除但不能被100整除,或者能被400整除,是闰年。(结果:输出闰年或平年)5.1.2任务要求(1)本题仅要求判断一个年份是否为闰年。5.1.3难点提示用?:运算符条件运算符是三目运算符,其格式为:表达式?语句1:语句2;其中表达式的值是布尔类型,…

    2022年7月17日
    10
  • CSDN日报20170304——《令人比较失落的IT圈子-关于华为裁员》[通俗易懂]

    CSDN日报20170304——《令人比较失落的IT圈子-关于华为裁员》[通俗易懂]早在几年前就有人说过程序员在35岁以后如果不做管理就很难混了,如今由于近日的华为事件被炒得沸沸扬扬,显然让这多年前人们的猜测变成了现实,我今年也正好到了这个该“退休”的年龄,所以就想趁机悔恨一番。首先,澄清的一点就是,我并无意诋毁这个IT行业,我只是希望大家可以更加清除的认清这个行业。

    2022年7月17日
    17
  • TCP和UDP的特点_TCP和UDP位于

    TCP和UDP的特点_TCP和UDP位于原文链接:https://www.jianshu.com/p/ef1811b3b44eOSI参考模型和TCP/IP协议群1.TCP/IP协议群的具体含义从字面意义上讲,有人可能会认为TCP/IP是指TCP和IP两种协议.实际生活中有时也确实就是指这两种协议.然而在很多情况下,它只是利用IP进行通信时所必须用到的协议群的统称.具体来说,IP或ICMP,TCP或U…

    2022年9月20日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号