Java递归实现评论多级回复

评论实体

数据库存储字段: id 评论idparent_id 回复评论idmessage 消息。其中如果评论不是回复评论,parent_id-1
创建一个评论实体 Comment

public class Comment {

    /**
     * id
     */
    private Integer id;

    /**
     * 父类id
     */
    private Integer parentId;

    /**
     * 消息
     */
    private String message;
    /**getter/setter ...**/
}

查询到所有的评论数据。方便展示树形数据,对Comment添加回复列表

List<ViewComment> children

ViewComment结构如下:

//展示树形数据
public class ViewComment {

    /**
     * id
     */
    private Integer id;

    /**
     * 父类id
     */
    private Integer parentId;

    /**
     * 消息
     */
    private String message;

    /**
     * 回复列表
     */
    private List<ViewComment> children = new ArrayList<>();
}

添加非回复评论

非回复评论的parent_id-1,先找到非回复评论:

List<ViewComment> viewCommentList = new ArrayList<>();
// 添加模拟数据
Comment comment1 = new Comment(1,-1,"留言1");
Comment comment2 = new Comment(2,-1,"留言2");
Comment comment3 = new Comment(3,1,"留言3,回复留言1");
Comment comment4 = new Comment(4,1,"留言4,回复留言1");
Comment comment5 = new Comment(5,2,"留言5,回复留言2");
Comment comment6 = new Comment(6,3,"留言6,回复留言3");

//添加非回复评论
for (Comment comment : commentList) {
    if (comment.getParentId() == -1) {
        ViewComment viewComment = new ViewComment();
        BeanUtils.copyProperties(comment,viewComment);
        viewCommentList.add(viewComment);
    }
}

递归添加回复评论

遍历每条非回复评论,递归添加回复评论:

for(ViewComment viewComment : viewCommentList) {
    add(viewComment,commentList);
}


private void add(ViewComment rootViewComment, List<Comment> commentList) {
    for (Comment comment : commentList) {
        // 找到匹配的 parentId  
        if (rootViewComment.getId().equals(comment.getParentId())) {
            ViewComment viewComment = new ViewComment();
            BeanUtils.copyProperties(comment,viewComment);
            rootViewComment.getChildren().add(viewComment);
            //递归调用 
            add(viewComment,commentList);
        }
    }
}

  • 遍历每条非回复评论。
  • 非回复评论id匹配到评论的parentId,添加到该评论的children列表中。
  • 递归调用。

demo

import org.springframework.beans.BeanUtils;

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

public class TestChilent {
    public static void main(String[] args) {
        List<Comment> commentList = new ArrayList<>();
        // 添加模拟数据
        Comment comment1 = new Comment(1, -1, "留言1");
        Comment comment2 = new Comment(2, -1, "留言2");
        Comment comment3 = new Comment(3, 1, "留言3,回复留言1");
        Comment comment4 = new Comment(4, 1, "留言4,回复留言1");
        Comment comment5 = new Comment(5, 2, "留言5,回复留言2");
        Comment comment6 = new Comment(6, 3, "留言6,回复留言3");

        commentList.add(comment1);
        commentList.add(comment2);
        commentList.add(comment3);
        commentList.add(comment4);
        commentList.add(comment5);
        commentList.add(comment6);

        List<ViewComment> viewCommentList = new ArrayList<>();

        //查询出首条评论
        for (Comment comment : commentList) {
            if (comment.getParentId() == -1){
                ViewComment viewComment = new ViewComment();
                BeanUtils.copyProperties(comment,viewComment);
                viewCommentList.add(viewComment);
            }
        }

        //为首条评论遍历,获取下面的子评论
        for (ViewComment viewComment : viewCommentList) {
            //通过首条评论去跟发布的所有评论比较,遍历
            traverse(commentList,viewComment);

        }


        for (ViewComment viewComment : viewCommentList) {
            System.out.println("viewComment = " + viewComment);
        }
    }

    private static void traverse(List<Comment> comment, ViewComment viewCommentList) {
        //遍历所有评论
        for (Comment com : comment) {
            //如果当前评论跟首条评论id和父id一致,代表是当前回合的评论
            if (com.getParentId() == viewCommentList.getId()){
                //将当前评论封装起来,放入首条评论的子选项中
                ViewComment viewComment = new ViewComment();
                BeanUtils.copyProperties(com,viewComment);
                viewCommentList.getChildren().add(viewComment);
                //继续遍历 所有评论和遍历好的首条评论,如果还有一致的继续放入子选项中
                traverse(comment,viewComment);
            }
        }
    }
}

展示结果

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值