Java 在android中,如何在for循环的每次迭代中向HashMap添加值

Java 在android中,如何在for循环的每次迭代中向HashMap添加值,java,android,hashmap,Java,Android,Hashmap,我有一个for循环,我正在为for循环内部的HashMap添加值。基本上我有一个帖子,上面有评论。一篇文章有8条评论,我需要HashMap的内容如下所示: PostId1, comment1 PostId1, comment2 PostId1, comment3 PostId1, comment4 PostId1, comment5 PostId1, comment6 PostId1, comment7 PostId1, comment8 但是,现在我的hashmap只包含第一个值。只有在第一

我有一个for循环,我正在为for循环内部的HashMap添加值。基本上我有一个帖子,上面有评论。一篇文章有8条评论,我需要HashMap的内容如下所示:

PostId1, comment1
PostId1, comment2
PostId1, comment3
PostId1, comment4
PostId1, comment5
PostId1, comment6
PostId1, comment7
PostId1, comment8
但是,现在我的hashmap只包含第一个值。只有在第一次迭代中,值才能添加到HashMap,如何在所有8次迭代中向HashMap添加值

PS:PostID对于所有八个值都应该相同

我当前的代码:

for (int i2 = 0; i2 < conversationArray.length(); i2++) {
    JSONObject conversationArray1 = conversationArray.getJSONObject(i2);
    contentConversation = conversationArray1.getString("content");
    commenterId = conversationArray1.getString("commenterId");
    commenterName = conversationArray1.getString("commenterName");
    commenterPhotos = conversationArray1.getString("commenterPhotos");
    postIdForComments = conversationArray1.getString("postId");
    lastDateUpdatedConversation = conversationArray1.getString("lastDateUpdated");
    dateCreatedConversation = conversationArray1.getString("dateCreated");
    commentDescription.add(contentConversation);
    commentUserName.add(commenterName);
    commentProfileImageLink.add(commenterPhotos);

    commentProfileImageHashMap.put(postIdForComments, commenterPhotos);
    commentDescriptionHashMap.put(postIdForComments, contentConversation);
    commentUserNameHashMap.put(postIdForComments, commenterName);
}
for(int i2=0;i2
请让我知道我应该在我的代码中做什么更改,以实现我的目标。欢迎所有建议。

使用
地图
存储每篇文章的评论列表:

MapMapofPosts=newHashMap();
List post1Comments=new ArrayList();
//收集某个帖子的评论
后1条。添加(“注释1”);
后1条。添加(“注释2”);
...
//将评论附在帖子上
posts.put地图(“post1”,post1Comments);
//对所有帖子重复此操作

您似乎不清楚HashMaps是如何工作的——条目对的第一项,即键,必须是唯一的,否则不应该使用HashMap。很可能您想要使用的是一个
Map
,它被实现为
HashMap
@HovercraftFullOfEels Ok。我会尽力去实现的。在我能像一个复制品一样结束之前就溜进了。相似的问题有这么多相似的答案,我们真的还需要一个吗?e、 该网站的点击率接近24000次,而现在该网站的点击率为24001次。
Map<String, List<String>> mapOfPosts = new HashMap<>();

List<String> post1Comments = new ArrayList<>();

// Collect comments of a certain post
post1Comments.add("comment1");
post1Comments.add("comment2");
...

// Attach comments to post
mapOfPosts.put("post1", post1Comments);

// Repeat this for all posts