HashMap has a method

 final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { ...} 

In which the key is checked for uniqueness. To determine the uniqueness, use the comparison of numbers by the operator & . Accordingly, the question:

What exactly does the & operator between two numbers do?

 12 & 0 //0 12 & 5 //4 12 & 12 //12 12 & 150 //4 15 & 67888795; //11 

    2 answers 2

    This is a bitwise AND operator.

      00101010 42 & 00001111 15 -------------- 00001010 10 

    Two numbers are represented in binary form. Then the & operator compares every bit of two numbers. How the AND operator works: if both numbers are 1, then the result is 1, if only one equals one or both zeros, then the result is 0. Then back from the binary form we convert to decimal and the usual number is obtained.

      This is a bitwise AND operation.

      From wiki :

      Bitwise AND is a binary operation whose operation is equivalent to applying a logical AND to each pair of bits that are in the same positions in the binary representations of the operands. In other words, if both corresponding bits of the operands are 1, the resulting binary bit is 1; if at least one bit of the pair is 0, the resulting binary bit is 0.

      • 15 and 67888795 are: 1111 and 100000010111110011010011011 And how is the number 11 obtained? - JAVAvladuxa
      • 7
        before 1111, the alignment with zeros occurs, 1011 remains from this operation, which is 11 in decimal - Komdosh