You can write that as a line of code in Java. There is no rule saying you have to use the result of the put method; you don't have to assign it to a variable or call one of its methods or anything. It's the same thing in the construct you're asking about.
So any value or null that is returned by lambda expression doesn't count as return type in Java?
You have to remember that lambda expressions are syntactical constructs only. That means they exist in that form only in source code. When compiled, they are actually objects that implement a functional interface. What functional interface that is depends on the context in which the lambda is declared. In your example, it would be a BiConsumer<String, Integer> implementation. Essentially, it is compiled as this:
The lambda expression is just shorthand for this, so it's easy for us as programmers to express the idea in a succinct way instead of drowning out the idea with all the syntactical infrastructure required by the language.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
henry leu wrote:. . . any value or null that is returned by lambda expression doesn't count as return type in Java?
I think you have misunderstood what Junilu said. He pointed out that a value returned is recorded inmemory somewhere, and if it isn't used it will simply be overwritten next time that memory area is used for something else. I am sure you ignore returned values all the time; I bet you have often written...and forgotten that you can write...instead. If a λ happens to have a method returning something, you can use that return value or forget about it. The only time you can't use it, but must ignore it, is when there is no return value. That means, when the functional method is declared as void.
posted 2 weeks ago
The only time you can't use it, but must ignore it, is when there is no return value. That means, when the functional method is declared as void.
Okay, but the BiConsumer functional method is declared as void.
As we discussed in this thread, the Java Language Specification specifically allows a functional interface with a void return type to be implemented by a statement expression, regardless of whether that expression returns a value.
JLS 15.27.3 wrote:A lambda expression is congruent with a function type if all of the following are
• The function type has no type parameters.
• The number of lambda parameters is the same as the number of parameter types of the function type.
• If the lambda expression is explicitly typed, its formal parameter types are the same as the parameter types of the function type.
• If the lambda parameters are assumed to have the same types as the function type's parameter types, then:
– If the function type's result is void, the lambda body is either a statement expression (§14.8) or a void-compatible block. – If the function type's result is a (non- void ) type R , then either (i) the lambda body is an expression that is compatible with R in an assignment context, or (ii) the lambda body is a value-compatible block, and each result expression (§15.27.2) is compatible with R in an assignment context.
This is my favorite show. And this is my favorite tiny ad:
Devious Experiments for a Truly Passive Greenhouse!