Home > Net >  Are java lambda functions unique (different hashCode and not equals)?
Are java lambda functions unique (different hashCode and not equals)?

Time:03-01

I am wondering if a java lambda function is always unique? If it is, what makes it unique?

In this pseudo code I have two Consumer with the some code but are not equals and hashCodes are differents too.

    Consumer<String> c1 = s -> {
        System.out.println(s);
    };
    Consumer<String> c2 = s -> {
        System.out.println(s);
    };

    System.out.println(c1.hashCode());  
    System.out.println(c2.hashCode());
    System.out.println(c1.equals(c2)); // false:  is it always false?

I want to store a list of lambda in a Set, and I am not sure if I will not lose some elements if by accident I got two lambda functions with equal hashCodes.

CodePudding user response:

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.

  • Related