需求: 回复分为两类, 一类是回复帖子的回复(这里记为0), 一类是回复 “回复” 的回复(这里记为1), 现在需要把这些回复构造成一颗树形回复, 也就是, '0’回复在树的第一层, '1’回复都在树的下层.
如图所示:
r代表树的根节点, 1,2,3代表’0’回复, 4, 5代表回复1的回复
下面是代码
//树类
import java.util.LinkedList;
import java.util.List;
public class ReplyTree
{
private ReplyVO reply;
private List<ReplyTree> children;
public ReplyTree()
{
this.reply = new ReplyVO();
this.children = new LinkedList<>();
}
public ReplyTree(ReplyVO reply)
{
this.reply = reply;
this.children = new LinkedList<>();
}
public ReplyVO getReply()
{
return reply;
}
public void setReply(ReplyVO reply)
{
this.reply = reply;
}
public List<ReplyTree> getChildren()
{
return children;
}
public void setChildren(List<ReplyTree> children)
{
this.children = children;
}
/**
* 向parent代表的节点下插入一个子节点
* @param parent
* @param reply
* @return
*/
public ReplyTree add(ReplyTree parent, ReplyVO reply)
{
ReplyTree child = new ReplyTree(reply);
parent.children.add(child);
return child;
}
/**
* 在parent的子结点中寻找reply是指定的reply的节点
* @param parent
* @param reply
* @return
*/
public ReplyTree findChild(ReplyTree parent, ReplyVO reply)
{
Integer id = reply.getReply().getId();
for (ReplyTree child : parent.getChildren())
{
if (child.getReply().getReply().getId() == id)
{
return child;
}
}
return null;
}
}
将list转为树的工具类
import com.lost_found.VO.ReplyTree;
import com.lost_found.VO.ReplyVO;
import com.lost_found.dao.UserMapper;
import com.lost_found.pojo.Reply;
import com.lost_found.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Component
public class TreeUtil
{
@Autowired
UserMapper userMapper;
public static TreeUtil treeUtil;
@PostConstruct
public void init()
{
treeUtil = this;
}
public static ReplyTree getTree(List<Reply> replyList)
{
ReplyTree parent = new ReplyTree();
List<Reply> list = new ArrayList<>(replyList);
TrasnferToTree(parent, replyList, list);
return parent;
}
/**
* 将list转化为树
* @param parent
* @param replyList
* @param list
*/
public static void TrasnferToTree(ReplyTree parent, List<Reply> replyList, List<Reply> list)
{
if (list.isEmpty())
return;
//因为需要边迭代边删除, 所以使用迭代器
Iterator<Reply> iterator = replyList.iterator();
while (iterator.hasNext())
{
Reply reply = iterator.next();
User user = treeUtil.userMapper.selectByPrimaryKey(reply.getUserId());
ReplyVO replyVO = new ReplyVO(reply, user.getHeadPortrait(), user.getNickName(), reply.getType());
//是回复帖子的回复, 而且现在的父节点为null, 则说明应该插入该节点
if (reply.getType() == 0 && parent.getReply().getReply() == null)
{
parent.add(parent, replyVO);
list.remove(reply);
if (isLeaf(replyVO, replyList))
continue;
TrasnferToTree(parent.findChild(parent, replyVO), replyList, list);
}
//如果是回复回复的回复且该节点的回复确实是回复父节点的回复, 则插入
else if (parent.getReply().getReply() != null && reply.getType() == 1 && reply.getReplyId() == parent.getReply().getReply().getId())
{
parent.add(parent, replyVO);
list.remove(reply);
TrasnferToTree(parent.findChild(parent, replyVO), replyList, list);
}
}
}
/**
* 判断是否是叶子节点
* @param replyVO
* @param replyList
* @return
*/
public static boolean isLeaf(ReplyVO replyVO, List<Reply> replyList)
{
boolean flag = true;
for (Reply reply : replyList)
{
if (reply.getReplyId() == replyVO.getReply().getId())
flag = false;
}
return flag;
}
}