• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Java | hashCode() Method
  1. hashCode() Method
  2. Example: Using Objects.hashCode

  1. hashCode() Method
    When implementing the hashCode() method, we should ensure that it follows the general contract: objects that are equal according to the equals(Object) method must have the same hash code.

    Additionally, it should be consistent (returning the same value during multiple invocations) and ideally distribute hash codes well to minimize collisions.

    Important: When overriding hashCode(), you must also override equals() to maintain the general contract that equal objects must have equal hash codes.

    The hashCode() method is defined in the java.lang.Object class. However, the default implementation does not necessarily return the object's internal address (this is implementation-dependent and may use identity hash codes):
    public native int hashCode();
    The java.util.Objects class provides utility methods to safely get the hash code of an object or multiple objects:
    public static int hashCode(Object o) {
        return o != null ? o.hashCode() : 0;
    }
    public static int hash(Object... values) {
        return Arrays.hashCode(values);
    }
    The java.util.Arrays class provides utility methods to get the hash code of arrays, including specific overloaded methods for each primitive type (e.g., hashCode(int[]), hashCode(boolean[]), etc.). It also provides deepHashCode(Object[]) for computing hash codes of nested arrays.
  2. Example: Using Objects.hashCode
    The following sample code demonstrates how to properly implement the hashCode() method.
    The implementation uses the hashCode() and hash utility methods from the java.util.Objects class:

    • Superclass "P":
      import java.util.Objects;
      
      class P {
          Integer id = 1;
      
          public Integer getId() {
              return id;
          }
      
          @Override
          public int hashCode() {
              return Objects.hashCode(id);
          }
      
          @Override
          public boolean equals(Object obj) {
              if (this == obj) {
                  return true;
              }
      
              if (!(obj instanceof P)) {
                  return false;
              }
      
              P p = (P) obj;
      
              return Objects.equals(this.getId(), p.getId());
          }
      }
    • Subclass "C":
      class C extends P {
          String code = "default";
      
          public String getCode() {
              return code;
          }
      
          @Override
          public int hashCode() {
              return Objects.hash(super.hashCode(), code);
          }
      
          @Override
          public boolean equals(Object obj) {
              if (!super.equals(obj))
                  return false;
      
              C c = (C) obj;
      
              return Objects.equals(this.getCode(), c.getCode());
          }
      }
© 2025  mtitek