递归用例
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);//递归调用 } } }
itjiandan