0

Let's say you have a custom class:

public class T {
    int a;
    int b;
}

Is there a way you can split up the class in a lambda expression in the following way:

(Stream of T instances).forEach((a, b) -> {});
3
  • 2
    stream.forEach(t -> {... t.a ... t.b ...});? Commented Jan 25, 2021 at 8:57
  • 1
    No, you cannot use a BiConsumer this way natively. Commented Jan 25, 2021 at 9:05
  • 1
    What are you actually trying to achieve? Commented Jan 25, 2021 at 9:11

1 Answer 1

2

Java doesn't have the concept of tuples, so no! But there can be a workaround by converting the stream to a Map (which has a lot of limitations and consequences):

(Stream of T instances)
    .collect(Collectors.toMap(t -> t.a, t -> t.b))
    .forEach((a, b) -> {});
Sign up to request clarification or add additional context in comments.

2 Comments

why convert to a Map even? and what limitations and consequences are related to OP's problem?
@Naman I certainly don't need to justify the limitations of converting stream to a map. You are free to post an answer that has BiConsumer rather than criticizing my post(as always).

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.