1

Is there a Java8 equivalent way of writing the following?

List<Foo> results = new ArrayList<>();
for (RowResult row : rows) {
   results.add(rowToFooFunction(classroomLookup.get(row.getId()), studentLookup.get(row.getId())).apply(row));
}

Where the rowToFooFunction is like Function<RowResult, Foo> rowToFoo (Classroom c, Student s)...

So I would like to end up with something like:

rows.stream.map(rowToFooFunction...).collect(Collectors.toList());

The problem is, in the map step, I need to lookup the student/classroom by the id of the row i am iterating over.

1
  • 1
    @SotiriosDelimanolis I'm sorry, what? Commented Sep 2, 2016 at 1:30

1 Answer 1

3

You almost found the solution yourself already, you are very close:

List<Foo> results = rows.stream().map(row ->
  rowToFooFunction(classroomLookup.get(row.getId()), studentLookup.get(row.getId())).apply(row))
).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

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.