1 回答

TA贡献1829条经验 获得超7个赞
我认为您需要遍历结果列表,当您找到具有父评论的结果时,您进入地图,获取父评论,将其计数加一并将其粘贴回地图中:
假设您的 Result 类是这样的:
class Result {
private String id;
private String parentCommentID;
public Result(String id, String parentCommentID) {
this.id = id;
this.parentCommentID = parentCommentID;
}
// GETTERS/SETTERS
}
你有一个包含 3 个结果的讨论列表
discussionsList = Arrays.asList(
new Result("1439", null),
new Result("1500", "1439"),
new Result("1801", "1439")
);
像你的情况一样, 1439 没有父母, 1500 和 1501 都有 1439 作为父母
然后你可以做这样的事情:
Map<String, Integer> commentsCountMap = new HashMap<>();
// Loop through the discussion Map
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the Result has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
}
}
System.out.println(commentsCountMap);
这输出
{1439=2}
没有找到匹配的内容?试试慕课网站内搜索吧
添加回答
举报