Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

600
Visualizações
Are java lambda functions unique (different hashCode and not equals)?

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda