According to JLS 15.27.4 (emphasis mine):
The value of a lambda expression is a reference to an instance of a class with the following properties:
- [...]
- The class overrides no other methods of the targeted functional interface type or other interface types mentioned above, although it may override methods of the
Object
class.
Since it says "may", the class of the lambda might have a custom implementation of equals
& hashCode
, or it might not, depending on the implementation.
Also note:
These rules are meant to offer flexibility to implementations of the
Java programming language, in that:
A new object need not be allocated on every evaluation.
Objects produced by different lambda expressions need not belong to
different classes (if the bodies are identical, for example).
Every object produced by evaluation need not belong to the same class
(captured local variables might be inlined, for example).
If an "existing instance" is available, it need not have been created
at a previous lambda evaluation (it might have been allocated during
the enclosing class's initialization, for example).
An implementation could potentially see that both of your lambdas have identical bodies, and produce just one class for your lambda. That class's equals
method could be overridden to return true
for any object that is also an instance of that same class. It could even use the same object for both of your lambdas! In either case, if you add both lambdas to your set, your set would only have one element.