2

Please can you help me resolve this issue I am having when attempting to stream through an array list and call a setter based on a method which returns a Boolean.

Written as a for loop, it would look like this:-

for (final PersonDto person : personList) {
    person.setUserCanEdit(userHasWriteRole(person));
}

private Boolean userHasWriteRole(final PersonDto person) {
        return getUserRoles().contains(getReadRole());
    }

I have tried a few variations with no success, along the following lines

final List<PersonDto> results = personList.stream().filter(a -> a.setUserCanEdit(this::userHasWriteRole)).collect(Collectors.toList());

... But it complains with The target type of this expression must be a functional interface

2
  • please make clear this private Boolean userHasWriteRole(final PersonDto person) { return getUserRoles().contains(getReadRole()); } Commented Jun 8, 2020 at 10:41
  • The method isn't really relevant, it has more logic within it, but for the purposes for this issue I wanted to show that it returns a Boolean value. Commented Jun 8, 2020 at 10:47

4 Answers 4

1

I think I would go for:

personList.stream()
   .filter(p -> userHasWriteRole(p))
   .forEach(p -> p.setUserCanEdit(true));

I think this keeps the intent clear,

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

Comments

0

Since you are updating the objects in array list, you can use forEach

personList.forEach(person ->person.setUserCanEdit(userHasWriteRole(person)));

6 Comments

I tried this but I get "Type mismatch: cannot convert from void to List<PersonDto>"
forEach method is void type @R111
so how do I accomplish the desired outcome?
personList will have the desired output, print the list and check it @R111
Thanks @Deadpool. It looks like that works, but can you tell me why I cant assign the output to a new arraylist? e.g.List<PersonDto> newList = personList.forEach(person ->person.setUserCanEdit(userHasWriteRole(person)));
|
0

The filter() method is an intermediate operation of the Stream interface that allows us to filter elements of a stream that match a given Predicate. You can't update data in the filter. Use forEach for this.

personList.stream().forEach(a -> a.setUserCanEdit(userHasWriteRole(a)));

And if you want to get in new arraylist make a copy of list and do this operations on new list

List<PersonDto> copy = new ArrayList<>(personList);
copy.stream().forEach(a -> a.setUserCanEdit(userHasWriteRole(a)));

Comments

0

If you want to have a list with filtered elements and do the setUserCanEdit on every elements in that list; you can do like this:

List<PersonDto> newList = personList.stream()
  .filter(p -> userHasWriteRole(p))
  .collect(Collectors.toList());

newList.forEach(p -> p.setUserCanEdit(true));

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.