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

524
Visualizações
why does Guava's Sets::hashCodeImpl have a weird loop update with double complement?

Guava computes the hash code of a set like this:

static int hashCodeImpl(Set<?> s) {
    int hashCode = 0;

    for(Iterator var2 = s.iterator(); var2.hasNext(); hashCode = ~(~hashCode)) {
        Object o = var2.next();
        hashCode += o != null ? o.hashCode() : 0;
    }

    return hashCode;
}

this is efficient and elegant - we use a commutative operation (addition) to "mix in" the hash codes of the objects, so that we get the same value regardless of order we iterate over the elements.

What I'm not sure about is the update condition in the for loop: hashCode = ~(~hashCode) seems to have no effect (IntelliJ's code inspector suggests to simplify the expression and then remove the self-assignment).

What's going on?

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

0

Although still a tad cryptic, the latest source code explains it (introduced by 86aa9f0):

hashCode = ~~hashCode;
// Needed to deal with unusual integer overflow in GWT.
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