码迷,mamicode.com
首页 > 其他好文 > 详细

递归用例

时间:2015-12-18 14:33:48      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

private void createReplyTree(List<CommentReplyVo> commentReplyVoList, List<CommentReply> replyList) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(replyList)) {
List<CommentReply> leftReplyList = new LinkedList<CommentReply>();
leftReplyList.addAll(replyList);

for (CommentReply reply : replyList) {
CommentReplyVo replyVo = transform(reply);
if (reply.getParentReplyId() == null) {//parentReplyId为NULL,则表示是对评论的回复
commentReplyVoList.add(replyVo);
leftReplyList.remove(reply);
}
}

addReplyTreeNode(commentReplyVoList, leftReplyList);
}
}

private void sortReplyTree(List<CommentReplyVo> commentReplyVoList, List<CommentReply> replyList) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(replyList)) {
List<CommentReply> leftReplyList = new LinkedList<CommentReply>();
leftReplyList.addAll(replyList);

for (CommentReply reply : replyList) {
CommentReplyVo replyVo = transform(reply);
if (reply.getParentReplyId() == null) {//parentReplyId为NULL,则表示是对评论的回复
commentReplyVoList.add(replyVo);
leftReplyList.remove(reply);
}
}

addReplyTreeNode(commentReplyVoList, leftReplyList);
}
}

private void addReplyTreeNode(List<CommentReplyVo> replyVoList, List<CommentReply> replyList) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(replyVoList) && org.apache.commons.collections.CollectionUtils.isNotEmpty(replyList)) {

List<CommentReply> leftReplyNodeList = new LinkedList<CommentReply>();
leftReplyNodeList.addAll(replyList);

for (CommentReplyVo replyVo : replyVoList) {
for (CommentReply reply : replyList) {
if (reply.getParentReplyId() != null && reply.getParentReplyId().equals(replyVo.getId())) {
replyVo.getCommentReplyVos().add(transform(reply));
leftReplyNodeList.remove(reply);
}
}
}

for (CommentReplyVo replyVo : replyVoList) {
addReplyTreeNode(replyVo.getCommentReplyVos(), leftReplyNodeList);//递归调用
}
}
}

 

递归用例

标签:

原文地址:http://www.cnblogs.com/itjiandan/p/5056833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!