Last active
January 26, 2026 09:10
-
-
Save glektarssza/821738e96ee5b7677cbba189c1293539 to your computer and use it in GitHub Desktop.
Java hashCode Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Source - https://stackoverflow.com/a/31220250 | |
| // Posted by Christopher Rucinski, modified by community. See post 'Timeline' for change history | |
| // Retrieved 2026-01-26, License - CC BY-SA 3.0 | |
| import java.util.Arrays; | |
| import javax.annotation.Nullable; | |
| public class HashCodeImpl { | |
| public boolean booleanField; | |
| public byte byteField; | |
| public char charField; | |
| public short shortField; | |
| public int intField; | |
| public long longField; | |
| public float floatField; | |
| public double doubleField; | |
| public Object[] arrayField; | |
| public Object referenceField; | |
| @Nullable | |
| public Object nullableReferenceField; | |
| @Override | |
| public int hashCode() { | |
| // Start with a non-zero constant. Prime is preferred | |
| int result = 17; | |
| // Primatives | |
| result = 31 * result + (booleanField ? 1 : 0); // 1 bit » 32-bit | |
| result = 31 * result + byteField; // 8 bits » 32-bit | |
| result = 31 * result + charField; // 16 bits » 32-bit | |
| result = 31 * result + shortField; // 16 bits » 32-bit | |
| result = 31 * result + intField; // 32 bits » 32-bit | |
| result = 31 * result + (int)(longField ^ (longField >>> 32)); // 64 bits » 32-bit | |
| result = 31 * result + Float.floatToIntBits(floatField); // 32 bits » 32-bit | |
| long doubleFieldBits = Double.doubleToLongBits(doubleField); // 64 bits (double) » 64-bit (long) » 32-bit (int) | |
| result = 31 * result + (int)(doubleFieldBits ^ (doubleFieldBits >>> 32)); | |
| // Objects | |
| result = 31 * result + Arrays.hashCode(arrayField); // var bits » 32-bit | |
| result = 31 * result + referenceField.hashCode(); // var bits » 32-bit (non-nullable) | |
| result = 31 * result + // var bits » 32-bit (nullable) | |
| (nullableReferenceField == null ? 0 : nullableReferenceField.hashCode()); | |
| return result; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| // Optimization (not required). | |
| if (this == o) { | |
| return true; | |
| } | |
| // Return false if the other object has the wrong type, interface, or is null. | |
| if (!(o instanceof HashCodeImpl)) { | |
| return false; | |
| } | |
| HashCodeImpl lhs = (HashCodeImpl) o; // lhs means "left hand side" | |
| // Primitive fields | |
| return booleanField == lhs.booleanField | |
| && byteField == lhs.byteField | |
| && charField == lhs.charField | |
| && shortField == lhs.shortField | |
| && intField == lhs.intField | |
| && longField == lhs.longField | |
| && floatField == lhs.floatField | |
| && doubleField == lhs.doubleField | |
| // Arrays | |
| && Arrays.equals(arrayField, lhs.arrayField) | |
| // Objects | |
| && referenceField.equals(lhs.referenceField) | |
| && (nullableReferenceField == null | |
| ? lhs.nullableReferenceField == null | |
| : nullableReferenceField.equals(lhs.nullableReferenceField)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment