1

I have a HashMap groups which maps to ArrayList objects for its values. I wanted to re-assign the values given they meet a particular condition (i.e. in this case, they are present in another list) and came across the forEach method. Here's how I implemented it. I could not find any documentation as to whether it is able to transform my data hence my question.

Here's the code:

groups.get(request.getGroup()).forEach(
          member -> {
               int updatedMemberIndex = request.getGroupMembers().indexOf(member);
               if (updatedMemberIndex != -1)
                     member = request.getGroupMembers().get(updatedMemberIndex);
          }
);

And here are the method signatures for the relevant methods in request object:

List<GroupMember> getGroupMembers()
Group getGroup()
Molly
  • 1,887
  • 3
  • 17
  • 34
Garikai
  • 405
  • 2
  • 15
  • 1
    Can you reassign references when you pass them to methods? – Bayleef Jan 22 '19 at 13:23
  • 1
    @Lino Be reasonable! No beginner can see the relationship between https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value and this question.This question has its value for being searchable by completely different wording. Anyone who was able to understand that these questions are more or less the same, would not need to ask this question at all. Let's be more educative and friendly here in SO! – Honza Zidek Jan 22 '19 at 13:32
  • @HonzaZidek still it's a duplicate, if they google something in the words of this question, they will then get redirected to the duplicate, which is a *must-read* when learning java. I don't quite understand why posting a duplicate would come along as unfriendly though, it's just the site-intended way I chose to help – Lino Jan 22 '19 at 13:33
  • @Lino Well, I do not agree that it is a duplicate. Behind almost ANY question there is an application of some of few very generic principles like this one. We would not need to ask almost at all if we were able to see it. *This* question is useful and *should not* be removed as a duplicate. (P.S. I work partially as a professional Java trainer.) – Honza Zidek Jan 22 '19 at 13:35
  • @HonzaZidek As I remember it, duplicates will not be removed, only off-topic questions with no answer will get removed (correct me if I am wrong). This means that this question will stay available and someone with a similar thought will be able to access this and can then decide if they want to click on the duplicate. To find many great answers and explanations – Lino Jan 22 '19 at 13:38
  • @Lino supposed they are able to see that *this* question is "just an application" of the mentioned general principle. As a better approach, I would prefer seeing an explaining answer which would **also** refer to your link and explain *how* they are connected. – Honza Zidek Jan 22 '19 at 13:41
  • @HonzaZidek I undestand your point, that the jump from this question to the duplicate may be a bit big. But the accepted answer already provides some good points so I see no real use to reopen this question to "just" post an appropriate answer which still would point to the duplicate – Lino Jan 22 '19 at 13:46

1 Answers1

4

No, you cannot mutate the collection in this way. member is a local variable, so reassigning it in the if doesn't do anything.

You could use member to change properties of the GroupMember object itself, but that's not what you want.

It looks like you're trying to perform a map operation. You probably want something like:

List<GroupMember> newList = groups.get(request.getGroup()).stream().map(
      member -> {
           int updatedMemberIndex = request.getGroupMembers().indexOf(member);
           return updatedMemberIndex != -1
               ? request.getGroupMembers().get(updatedMemberIndex)
               : member;
      }
).collect(Collectors.toList());
Willis Blackburn
  • 8,068
  • 19
  • 36