如何用JAVA实现平级变树?

像这种数据,现在想转成树结构返回给前端。分为123级,每一级都去重

这个问题其实很简单,不废话,直接上代码……

img

        //此处我为了方便,数据库创建了表,查询表得到数据
        List<Student> studentList = this.list();
        if(!CollectionUtils.isEmpty(studentList)){
           //使用JDK1.8新特性对数据进行多级分组
           Map<String,Map<String,List<Student>>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName,Collectors.groupingBy(Student::getSubject)));
        }

img


轻松搞定,当然还有一些细节你可以进行优化处理,希望能帮到你哦……