TREEUTIL


package com.user.util;


import com.user.model.entity.CommentEntity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*
 * 构架树结构树形json--好用
 * */
public class TreeUtil {

    /**
     * 根据pid,构建树节点
     */
    public static <T extends CommentEntity> List<T> build(List<T> treeNodes, Long pid) {

        List<T> treeList = new ArrayList<>();
        for(T treeNode : treeNodes) {
            if (pid.equals(treeNode.getParentCommentId())) {
                treeList.add(findChildren(treeNodes, treeNode));
            }
        }
        return treeList;
    }

    /**
     * 查找子节点
     */
    private static <T extends CommentEntity> T findChildren(List<T> treeNodes, T rootNode) {
        for(T treeNode : treeNodes) {
            if(rootNode.getId().equals(treeNode.getParentCommentId())) {
                rootNode.getChildren().add(findChildren(treeNodes, treeNode));
            }
        }
        return rootNode;
    }

    /**
     * 构建树节点
     */
    public static <T extends CommentEntity> List<T> build(List<T> treeNodes) {
        List<T> result = new ArrayList<>();
        //list转map
        Map<Long, T> nodeMap = new LinkedHashMap<>(treeNodes.size());
        for(T treeNode : treeNodes){
            nodeMap.put(treeNode.getId(), treeNode);
        }
        for(T node : nodeMap.values()) {
            T parent = nodeMap.get(node.getParentCommentId());
            if(parent != null && !(node.getId().equals(parent.getId()))){
                parent.getChildren().add(node);
                continue;
            }

            result.add(node);
        }
        return result;
    }

    /**
     * 获取某个父节点下面的所有子节点
     * @param menuList
     * @param pid
     * @return
     */
    public static List<CommentEntity> treeChildrenList( List<CommentEntity> menuList, Long pid){
        List<CommentEntity> childMenu=new ArrayList<CommentEntity>();

        for(CommentEntity mu: menuList){
            //遍历出父id等于参数的id,add进子节点集合
            if(mu.getParentCommentId()==pid){
                //递归遍历下一级
                treeChildrenList(menuList,mu.getParentCommentId());
                childMenu.add(mu);
            }
        }
        return childMenu;
    }

}

package com.core.model.entity.user;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.core.model.common.BaseEntity;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.mybaits.jpa.annotation.DaoClass;
import com.core.dao.CommentDao;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论回复信息
 */
@TableName(value = "t_comment")
@DaoClass(daoClass = CommentDao.class)
@ApiModel(value = "评论回复信息")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class CommentEntity extends BaseEntity implements Serializable {

    /**内容评论人姓名*/
    private String userName;

    /**文章id*/
    private Long  articleId;

    /**父评论id*/
    private Long parentCommentId;

    /**评论的内容*/
    private String  content;

    /**是否有效*/
    private Integer  isValid;

    /**点赞数*/
    private Integer  praiseNum;

    /**是否一级评论 1 是 2 否*/
    private Integer  isTop;


    @TableField(exist = false)
    List<CommentEntity> children = new ArrayList<CommentEntity>();


}

 使用 注意 实体类一定要有parentId 和 id

// List<实体类>
TreeUtil.build( List<实体类>)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值