0

I have an arraylist of Comments, which each comment has a specific id and content. I have a modifiedComment with the same id, but with different content, how would I replace it in the comments array? Here is what I tried, I get the comment out, update the content, but don't know how to put it back into the array... The comment class has id and content as attributes.

  ArrayList<Comment> comments = foundQuestion.getComments();
  // found a comment with specific id
  Comment foundComment = comments.stream().filter(comment1 -> comment1.getId().equals(modifiedComment.getId())).collect(Collectors.toList()).get(0);
  // update the content of the current comment 
  foundComment.setContent(modifiedComment.getContent());

I would really appreciate any help.

2
  • 1
    please edit the question and show definition of Comment class. Commented Dec 22, 2020 at 16:14
  • It seems your design could use a re-think. You have a one-to-many relationship of entities: A Comment that has an identifier but no content, and a collection of Draft objects with their own identifier and holding content, children that belong to a Comment parent. The latest Draft child object is the current content for the parent Comment. Commented Dec 28, 2020 at 1:37

6 Answers 6

2

You.. don't have to.

Objects in java are like treasure buried in the sand, and almost everything in javaland doesn't work on treasure. It works on treasure maps. That list of yours? It's a list of treasure maps. Not of treasure. The . operator is the 'follow the map and dig' operator.

What you've done here is find the treasure map where 'X' marks the spot where, if you dig, you find the comment with the desired id. You get a copy of this map (Comment foundComment is this copy).

You then take the copy of this treasure map, walk to the X, dig down, open the box, and find inside: the content (or, rather, a treasure map to it). You toss it out and replace it with different content.

There's no need to take your copy of the map and put it back anywhere. You did not modify the treasure map. You used it to find treasure and mess with it. Anything else that has a copy of your map can follow theirs and see the effect of your rummaging through the chest.

NB: As a sidenote, the general job of what you're doing here is best done with a hashmap. The API is not just a lot cleaner, it's also orders of magnitude more efficient (specifically, map ops are O(1) or O(logn), whereas your plan is O(n).

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry it’s a part of a web app that I built so I would like to still keep it as an arraylist.
@user545871 This Answer gives wise advice: Your problem calls for a Map as a solution, not a List.
1

Cant you just take your modifiedComment, delete the origingal foundComment and add the modifiedComment back to your arraylist with append() ?

2 Comments

Thank you, but that won’t preserve the order
Then simply save the index of the foundComment and insert the modifiedcomment again at that index with java.util.ArrayList.add()
1

Iterate the comments arrayList with a check of foundComment.getId().equals(comment.getId()). If it matches get the content value from foundComment and put it in comments like below

  for(Comment comment:comments){
    if(comment.getId().equals(foundComment.getId())){
    comment.setContent(foundComment.getContent());
    }         
   }

Comments

1

No need to use streams, just iterate over the array and modify the object or its content.

Modifying Comment content:

ArrayList<Comment> comments = foundQuestion.getComments();
for (Comment comment : comments) {
    if (comment.getId().equals(modifiedComment.getId()) {
        comment.setContent(modifiedComment.getContent());
        break;
    }
}

Replacing the complete Comment object:

ArrayList<Comment> comments = foundQuestion.getComments();
for (int i = 0; i < comments.size(); ++i) {
    if (comments.get(i).getId().equals(modifiedComment.getId()) {
        comments.set(i, modifiedComment);
        break;
    }
}

Comments

0

You can just use a for each after filtering (without collecting to a list as you did in your example)

ArrayList<Comment> comments = foundQuestion.getComments();
Comment mod = //your modified comment;
comments.stream()
            .filter(com -> com.getId().equals(mod.getId())
            .forEach(com -> com.setContent(mod.getContent()));

Comments

0

You can do it like this. Just replace the modified comment in the stream if equal, otherwise keep the original. This presumes that the id is an object and not a primitive int. Otherwise you should use ==.

 comments = foundQuestion.getComments().stream()
        .map(comment -> comment.getId()
                .equals(modifiedComment.getId()) ?
                        modifiedComment : comment)
        .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.