Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能
最近工作需要做一個(gè)評(píng)論功能,除了展示評(píng)論之外,還需要展示評(píng)論回復(fù),評(píng)論的回復(fù)的回復(fù),這里就用到了遞歸實(shí)現(xiàn)評(píng)論的多級(jí)回復(fù)。
評(píng)論實(shí)體
數(shù)據(jù)庫(kù)存儲(chǔ)字段: id 評(píng)論id、parent_id 回復(fù)評(píng)論id、message 消息。其中如果評(píng)論不是回復(fù)評(píng)論,parent_id 為-1。
創(chuàng)建一個(gè)評(píng)論實(shí)體 Comment:
public class Comment {
/**
* id
*/
private Integer id;
/**
* 父類id
*/
private Integer parentId;
/**
* 消息
*/
private String message;
}查詢到所有的評(píng)論數(shù)據(jù)。方便展示樹(shù)形數(shù)據(jù),對(duì)Comment添加回復(fù)列表
List<ViewComment> children
ViewComment結(jié)構(gòu)如下:
// 展示樹(shù)形數(shù)據(jù)
public class ViewComment {
/**
* id
*/
private Integer id;
/**
* 父類id
*/
private Integer parentId;
/**
* 消息
*/
private String message;
/**
* 回復(fù)列表
*/
private List<ViewComment> children = new ArrayList<>();
}
添加非回復(fù)評(píng)論
非回復(fù)評(píng)論的parent_id為-1,先找到非回復(fù)評(píng)論:
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,回復(fù)留言1");
Comment comment4 = new Comment(4,1,"留言4,回復(fù)留言1");
Comment comment5 = new Comment(5,2,"留言5,回復(fù)留言2");
Comment comment6 = new Comment(6,3,"留言6,回復(fù)留言3");
//添加非回復(fù)評(píng)論
for (Comment comment : commentList) {
if (comment.getParentId() == -1) {
ViewComment viewComment = new ViewComment();
BeanUtils.copyProperties(comment,viewComment);
viewCommentList.add(viewComment);
}
}遞歸添加回復(fù)評(píng)論
遍歷每條非回復(fù)評(píng)論,遞歸添加回復(fù)評(píng)論:
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);
}
}
}- 遍歷每條非回復(fù)評(píng)論。
- 非回復(fù)評(píng)論
id匹配到評(píng)論的parentId,添加到該評(píng)論的children列表中。 - 遞歸調(diào)用。
結(jié)果展示:

github 源碼
https://github.com/jeremylai7/java-codes/tree/master/basis/src/main/java/recurve
到此這篇關(guān)于Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)的文章就介紹到這了,更多相關(guān)Java評(píng)論多級(jí)回復(fù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解java連接mysql數(shù)據(jù)庫(kù)的五種方式
這篇文章主要介紹了詳解java連接mysql數(shù)據(jù)庫(kù)的五種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java項(xiàng)目部署的完整流程(超詳細(xì)!)
我相信很多Java新手都會(huì)遇到這樣一個(gè)問(wèn)題,跟著教材敲代碼,很容易,但是讓他完整的實(shí)現(xiàn)一個(gè)應(yīng)用項(xiàng)目卻不會(huì),下面這篇文章主要給大家介紹了關(guān)于Java項(xiàng)目部署的完整流程,需要的朋友可以參考下2022-07-07
SpringBoot自定義全局異常處理器的問(wèn)題總結(jié)
Springboot框架提供兩個(gè)注解幫助我們十分方便實(shí)現(xiàn)全局異常處理器以及自定義異常,處理器會(huì)優(yōu)先處理更具體的異常類型,如果沒(méi)有找到匹配的處理器,那么它會(huì)尋找處理更一般異常類型的處理器,本文介紹SpringBoot自定義全局異常處理器的問(wèn)題,一起看看吧2024-01-01
Java使用Calendar類實(shí)現(xiàn)動(dòng)態(tài)日歷
這篇文章主要為大家詳細(xì)介紹了Java使用Calendar類實(shí)現(xiàn)動(dòng)態(tài)日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問(wèn)題解決
這篇文章主要介紹了java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
詳解Java8?CompletableFuture的并行處理用法
Java8中有一個(gè)工具非常有用,那就是CompletableFuture,本章主要講解CompletableFuture的并行處理用法,感興趣的小伙伴可以了解一下2022-04-04
Java替換字符串replace和replaceAll方法舉例詳解
這篇文章主要介紹了Java中替換字符串的幾種方法,包括String類的replace()、replaceAll()、replaceFirst()方法,以及StringBuilder和StringBuffer類的replace()方法,還提到了一些第三方庫(kù),如Hutool,它們提供了更豐富的字符串處理功能,需要的朋友可以參考下2025-02-02
一篇文章帶你了解一些Java反射的學(xué)習(xí)記錄
java反射機(jī)制是一個(gè)很好用的東西,用它可以解決很多死的東西,因?yàn)榉瓷錂C(jī)制的靈活行很大,有了他,我們就不要花太多的時(shí)間來(lái)寫(xiě)操做數(shù)據(jù)庫(kù)的代碼了,這個(gè)可以很大的減少開(kāi)發(fā)時(shí)間,而且代碼的可讀性好2021-09-09
SpringBoot2.x中management.security.enabled=false無(wú)效的解決
這篇文章主要介紹了SpringBoot2.x中management.security.enabled=false無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

