lmngn23
lmngn23

Reputation: 521

Modify arraylist object in Java

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.

Upvotes: 0

Views: 86

Answers (6)

WJS
WJS

Reputation: 40047

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());

Upvotes: 0

Eritrean
Eritrean

Reputation: 16508

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()));

Upvotes: 0

Cristian Ramon-Cortes
Cristian Ramon-Cortes

Reputation: 1888

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;
    }
}

Upvotes: 1

Ramesh Bathini
Ramesh Bathini

Reputation: 43

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());
    }         
   }

Upvotes: 1

Lucas Goldner
Lucas Goldner

Reputation: 730

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

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 103773

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).

Upvotes: 2

Related Questions