0

My list consist of list character definition like List<Character> listCharacter = new ArrayList<Character>();

Character class:

private List<Result> results;

Result Class :

 private int id;

 private String name;

I am trying to iterate over listCharacter like

listCharacter.forEach((characters) -> {

            characters.getResults().forEach((result) -> {

                if (result.getId() == id) {
                    return result;
                }

            });
        });

But when am trying this i got foreach not applicable the type Iterable is not applicable for the arguments (( result) -> {}) error . I know chain loop not possible with foreach loop.

Also i know we can use consumers like duplicate question solutions.But then i can't reach outer loop variable inside inner loop.The Consumer classes just using it and disposing it.Therefore I don't wanna use that.

How can i do this i mean reaching outer loop variable inside inner loop without dealing with this such errors?

TLDR: I have 2 list objects. I am iterating over outer one(listCharacter) that who has inner one list object(result) which has id and name.If the id matched the method would return.That's all.

5
  • forEach doesn't return. I think you want to map possibly you want to flatMap. Commented May 28, 2020 at 21:24
  • I would definitely avoid calling a custom class Character as that already exists in the Java API. consider changing it to something more meaningful. currently private Result results; is not iterable . is that suppose to be a List<Result> results instead within Character class?. Commented May 28, 2020 at 21:29
  • I am sorry yes results type List<Result> i updated my question Commented May 28, 2020 at 21:50
  • Is character id unique across all characters? (If so, there is a much simpler approach!) Commented May 28, 2020 at 21:54
  • Yes they all are unique . Could you share with me your idea ? Commented May 28, 2020 at 22:01

2 Answers 2

3

I would firstly suggest that you avoid calling a custom class Character as that's already in use by the Java API. you don't have to but it would avoid confusion here and there. Consider remaining to something more meaningful and reflecting of what the class does.

Also note that you cannot return a result from forEach.

You can accomplish your intended result as follows:

source.stream()
      .map(Character::getResults)
      .flatMap(Collection::stream)
      .filter(s -> s.getId() == id)
      .findFirst().orElse(null);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much.For learning why we mapped this result list ? Also with flatmap i know flatmap for having filter, sum, distinct operations. But could'nt we do that without flatmap in this case?
0

"results" is not a list or any other iterator, it's just a single object of type "Result". Thus, you can not iterate over it at all. Make sure getResults() actually returns a list, rather than a single object, and then you can iterate over it.

(Also note that forEach() is not actually a loop. It's a function call with a lambda function as a parameter. And as mentioned before, you can not use forEach() to collect some kind of result from the lambda function, you'll have to use map() for that.)

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.