Java遞歸實現(xiàn)評論多級回復功能
最近工作需要做一個評論功能,除了展示評論之外,還需要展示評論回復,評論的回復的回復,這里就用到了遞歸實現(xiàn)評論的多級回復。
評論實體
數(shù)據(jù)庫存儲字段: id 評論id、parent_id 回復評論id、message 消息。其中如果評論不是回復評論,parent_id 為-1。
創(chuàng)建一個評論實體 Comment:
public class Comment {
/**
* id
*/
private Integer id;
/**
* 父類id
*/
private Integer parentId;
/**
* 消息
*/
private String message;
}查詢到所有的評論數(shù)據(jù)。方便展示樹形數(shù)據(jù),對Comment添加回復列表
List<ViewComment> children
ViewComment結(jié)構(gòu)如下:
// 展示樹形數(shù)據(jù)
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<>();
// 添加模擬數(shù)據(jù)
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);
//遞歸調(diào)用
add(viewComment,commentList);
}
}
}- 遍歷每條非回復評論。
- 非回復評論
id匹配到評論的parentId,添加到該評論的children列表中。 - 遞歸調(diào)用。
結(jié)果展示:

github 源碼
https://github.com/jeremylai7/java-codes/tree/master/basis/src/main/java/recurve
到此這篇關(guān)于Java遞歸實現(xiàn)評論多級回復的文章就介紹到這了,更多相關(guān)Java評論多級回復內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解java連接mysql數(shù)據(jù)庫的五種方式
這篇文章主要介紹了詳解java連接mysql數(shù)據(jù)庫的五種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
SpringBoot自定義全局異常處理器的問題總結(jié)
Springboot框架提供兩個注解幫助我們十分方便實現(xiàn)全局異常處理器以及自定義異常,處理器會優(yōu)先處理更具體的異常類型,如果沒有找到匹配的處理器,那么它會尋找處理更一般異常類型的處理器,本文介紹SpringBoot自定義全局異常處理器的問題,一起看看吧2024-01-01
Java使用Calendar類實現(xiàn)動態(tài)日歷
這篇文章主要為大家詳細介紹了Java使用Calendar類實現(xiàn)動態(tài)日歷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
java本機內(nèi)存分配Native?memory?allocation?mmap失敗問題解決
這篇文章主要介紹了java本機內(nèi)存分配Native?memory?allocation?mmap失敗問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
詳解Java8?CompletableFuture的并行處理用法
Java8中有一個工具非常有用,那就是CompletableFuture,本章主要講解CompletableFuture的并行處理用法,感興趣的小伙伴可以了解一下2022-04-04
Java替換字符串replace和replaceAll方法舉例詳解
這篇文章主要介紹了Java中替換字符串的幾種方法,包括String類的replace()、replaceAll()、replaceFirst()方法,以及StringBuilder和StringBuffer類的replace()方法,還提到了一些第三方庫,如Hutool,它們提供了更豐富的字符串處理功能,需要的朋友可以參考下2025-02-02
SpringBoot2.x中management.security.enabled=false無效的解決
這篇文章主要介紹了SpringBoot2.x中management.security.enabled=false無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

