将List对象列表转为树形结构

1、SocialTag.java

package com.nijia.vip.social.entity;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.nijia.base.entity.BaseEntity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 社交--个人标签(SocialTag)表实体类
 *
 * @author whp
 * @since 2022-06-23 14:15:53
 */
@SuppressWarnings("serial")
@Data
public class SocialTag extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.ASSIGN_ID)
    /**
     * 主键
     */
    @ApiModelProperty("主键")
    private Long id;

    /**
     * 标签名称
     */
    @ApiModelProperty("标签名称")
    private String tagName;

    /**
     * 父级id
     */
    @ApiModelProperty("父级id")
    private Long parentId;

    /**
     * 标签等级【1-1级,2-2级】
     */
    @ApiModelProperty("标签等级【1-1级,2-2级】")
    private Integer levelNo;

    /**
     * 排序号
     */
    @ApiModelProperty("排序号")
    private Integer sortNo;

    /**
     * 可用状态【1-可用,0-不可用】
     */
    @ApiModelProperty("可用状态【1-可用,0-不可用】")
    private Integer enableFlag;

    /**
     * 删除标识【1-删除,0-正常】
     */
    @ApiModelProperty("删除标识【1-删除,0-正常】")
    private Integer deleteFlag;

    @TableField(exist=false)
    private List<SocialTag> childSocialTagList = new ArrayList<SocialTag>();


}

 

2、SocialTagListToTreeTest.java

package com.nijia.vip.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;

import com.nijia.test.BaseTest;
import com.nijia.vip.social.entity.SocialTag;

import io.geekidea.springbootplus.framework.util.JavaJsonConvert;


public class SocialTagListToTreeTest extends BaseTest {
    
     public static void main(String[] args) {
         List<SocialTag> resultList = convertListToTree(initData());
         System.out.println(JavaJsonConvert.javaToJson(resultList));
    }

    
     /**
      * 将List对象列表转为树形结构
      * 
      * @param socialTagList
      * @return
      */
    private static List<SocialTag> convertListToTree(List<SocialTag> socialTagList) {
        List<SocialTag> resultList = new ArrayList<SocialTag>();
        if(CollectionUtils.isEmpty(socialTagList)) {
            return resultList;
        }
        
        //List 转为 Map
         Map<Long, SocialTag> socialTagMap = socialTagList.stream().collect(Collectors.toMap(SocialTag::getId, item -> item));
         for(SocialTag socialTag : socialTagList) {
             SocialTag parentSocialTag = socialTagMap.get(socialTag.getParentId());
             //该节点为空,说明是顶级节点
             if(parentSocialTag == null) {
                 // 将顶级节点加入结果集中
                 resultList.add(socialTag);
                 continue;
             }
             
             // 把自己加到父节点对象里面去
             parentSocialTag.getChildSocialTagList().add(socialTag);
         }
         
        return resultList;
    }

    private static List<SocialTag> initData() {
        List<SocialTag> socialTagList = new ArrayList<SocialTag>();
        SocialTag socialTag = new SocialTag();
        socialTag.setId(1L);
        socialTag.setTagName("Java");
        socialTag.setParentId(0L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(1);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(10L);
        socialTag.setTagName("Springboot");
        socialTag.setParentId(1L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(2);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(11L);
        socialTag.setTagName("SpringCloud");
        socialTag.setParentId(1L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(2);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(2L);
        socialTag.setTagName("DB");
        socialTag.setParentId(0L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(1);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(20L);
        socialTag.setTagName("Mysql");
        socialTag.setParentId(2L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(2);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(21L);
        socialTag.setTagName("Oracle");
        socialTag.setParentId(2L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(2);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(3L);
        socialTag.setTagName("JS");
        socialTag.setParentId(0L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(1);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        socialTag = new SocialTag();
        socialTag.setId(30L);
        socialTag.setTagName("VUE");
        socialTag.setParentId(3L);
        //标签等级【1-1级,2-2级】
        socialTag.setLevelNo(2);
        socialTag.setCreateTime(new Date());
        socialTagList.add(socialTag);
        
        return socialTagList;
    }

   
}

 

posted on 2022-06-25 12:15  Ruthless  阅读(698)  评论(0编辑  收藏  举报