將List物件列表轉為樹形結構

Ruthless發表於2022-06-25

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;
    }

   
}

 

相關文章