What is the reason for using ^ in a hashcode method?

Gnoupi

I found this piece of code today:

private static class Node{
    private final int code1;
    private final int code2;

    public Node(int code1, int code2) {
        this.code1 = code1;
        this.code2 = code2;
    }

    @Override
    public int hashCode() {
        return (code1 * 31) ^ code2;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Node) {
            Node node = (Node) obj;
            return node.code1 == code1 && node.code2 == code2;
        }
        return false;
    }
}

Is there a good reason for the hashcode uses (code1 * 31) ^ code2; ?

From my understanding, the usual way to create a hashcode is to multiply the current hashcode (or the first field) by a prime number, and add the next field, for every field.

Why would there be a need for a power XOR operator here?

Peter Lawrey

The ^ is not a power, it is bit wise exclusive OR. The purpose of a hash code is to have a random distribution and ^ has slightly better properties than + but not enough that most people care.

BTW There is no power operator in Java, only Math.pow(a, b)


I am probably wrong but, the way I remember it is;

You want an operation which has an equal chance of producing 0 or 1 for each bit. This excludes multiplication, division, modulus, and, or, or shifting. This leaves ^, -, or +. When you overflow you lose a bit of information and ^ is the only one which doesn't overflow.

In the example above, the codes might not be larger enough to overflow, and even if you use + instead, I expect the difference to be very small.


I am not mathematical expert on the subject, but to me + seems a better option. If you have the following (using 0-255 bytes, but it should be representative or what happens with int values)

int[] add = new int[256];
int[] xor = new int[256];

for (int i = 0; i < 256; i++)
    for (int j = 0; j < 256; j++) {
        add[(i + j) & 0xFF]++;
        xor[i ^ j]++;
    }

System.out.println("add: " + Arrays.toString(add));
System.out.println("xor: " + Arrays.toString(xor));

you have an equal chance of every value occurring.

add: [256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256]
xor: [256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256]

if we assume a biased distribution, such as the range of ASCII values, 0 to 127, there is a chance a bit which is not set in either value will be set with add, but not xor.

add: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
xor: [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

and you take a modulus, of say 37, the distribution appears more even with + (The difference between the most common and least common result is smaller)

add: [243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 237, 237, 237, 237, 237, 238, 239, 240, 241, 242, 0, 0, ..

xor: [283, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 218, 218, 218, 218, 218, 220, 220, 220, 220, 220, 220, 220, 220, 220, 220, 188, 188, 188, 188, 188, 0, 0, ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What happening in Object.hashCode() method?

From Dev

what is the reason helper classes method declared in static

From Dev

What is the reason for this method to take so much memory?

From Dev

what is the reason helper classes method declared in static

From Dev

hashCode() method

From Java

Reason for using CharSequence as parameter in String replace method

From Dev

what is the internal working of System.idendityHashCode() and hashCode() method?

From Dev

What hashing method does the object.hashCode use?

From Dev

What is HashCodeBuilder and EqualsBuilder which is used in overriding the hashcode() and equals() method?

From Dev

What is the reason to use these anonymous block to call super class method in this code?

From Dev

What is the reason behind "get" method implementation of AtomicMarkableReference in java?

From Dev

What is the reason behind CS1998 "method lacks await operators"

From Dev

What is the reason for compile warning "static method declared final"?

From Dev

What is the reason behind null checks in method reference expression evaluation?

From Dev

What is the reason behind CS1998 "method lacks await operators"

From Dev

Is there a reason I should call Integer.hashCode()?

From Dev

Designing hashCode method Java

From Dev

Use String hashCode() Method?

From Dev

Serialization With HashCode Method

From Dev

Why to override hashCode method?

From Dev

Alphanumeric hashCode method

From Dev

Will this hashCode method overflow?

From Dev

Overriding hashcode method to return hashcode of variable in a class

From Dev

What is a reason for using shift $((OPTIND-1)) after getopts?

From Dev

What's the reason for storing data with .data() instead of using plain variables?

From Dev

What's the reason for using lambda expressions to define functions in Scheme?

From Dev

What is the reason behind using OPTION request before POST on CORS requests?

From Dev

What's the reason of using Circular Buffer in iOS Audio Calling APP?

From Dev

What's the reason behind using routes file in modern frameworks?

Related Related

  1. 1

    What happening in Object.hashCode() method?

  2. 2

    what is the reason helper classes method declared in static

  3. 3

    What is the reason for this method to take so much memory?

  4. 4

    what is the reason helper classes method declared in static

  5. 5

    hashCode() method

  6. 6

    Reason for using CharSequence as parameter in String replace method

  7. 7

    what is the internal working of System.idendityHashCode() and hashCode() method?

  8. 8

    What hashing method does the object.hashCode use?

  9. 9

    What is HashCodeBuilder and EqualsBuilder which is used in overriding the hashcode() and equals() method?

  10. 10

    What is the reason to use these anonymous block to call super class method in this code?

  11. 11

    What is the reason behind "get" method implementation of AtomicMarkableReference in java?

  12. 12

    What is the reason behind CS1998 "method lacks await operators"

  13. 13

    What is the reason for compile warning "static method declared final"?

  14. 14

    What is the reason behind null checks in method reference expression evaluation?

  15. 15

    What is the reason behind CS1998 "method lacks await operators"

  16. 16

    Is there a reason I should call Integer.hashCode()?

  17. 17

    Designing hashCode method Java

  18. 18

    Use String hashCode() Method?

  19. 19

    Serialization With HashCode Method

  20. 20

    Why to override hashCode method?

  21. 21

    Alphanumeric hashCode method

  22. 22

    Will this hashCode method overflow?

  23. 23

    Overriding hashcode method to return hashcode of variable in a class

  24. 24

    What is a reason for using shift $((OPTIND-1)) after getopts?

  25. 25

    What's the reason for storing data with .data() instead of using plain variables?

  26. 26

    What's the reason for using lambda expressions to define functions in Scheme?

  27. 27

    What is the reason behind using OPTION request before POST on CORS requests?

  28. 28

    What's the reason of using Circular Buffer in iOS Audio Calling APP?

  29. 29

    What's the reason behind using routes file in modern frameworks?

HotTag

Archive