Java中的hashCode方法

Java中的hashCode方法,java,hashmap,hashcode,Java,Hashmap,Hashcode,这是正常的,我有相同的哈希代码为这两个??我很困惑。我认为hashcode是独一无二的 public static void main(String[] args) { HashMap<String, Integer> t = new HashMap<String, Integer>(); t.put("one", 123); t.put("two", 123); System.out.println(t.get("one").hash

这是正常的,我有相同的哈希代码为这两个??我很困惑。我认为hashcode是独一无二的

public static void main(String[] args) {
    HashMap<String, Integer> t = new HashMap<String, Integer>();

    t.put("one", 123);
    t.put("two", 123);

    System.out.println(t.get("one").hashCode());
    System.out.println(t.get("two").hashCode());
}

是,
123
123
具有相同的hashcode,因为它们是具有相同
int
值的两个:

t.get("one") // returns an Integer with an int value of 123
t.get("two") // returns an Integer with an int value of 123
从(
Integer.hashCode()
)中:

此对象的哈希代码值,等于基元int值 由这个整数对象表示

如有疑问,请使用来源:

     /**
  743        * Returns a hash code for this {@code Integer}.
  744        *
  745        * @return  a hash code value for this object, equal to the
  746        *          primitive {@code int} value represented by this
  747        *          {@code Integer} object.
  748        */
  749       public int hashCode() {
  750           return value;
  751       }

相等的强文本对象在运行的进程中必须具有相同的哈希代码

请注意,这并不意味着以下常见误解:

Unequal objects must have different hash codes – WRONG!
Objects with the same hash code must be equal – WRONG!

契约允许不相等的对象共享相同的哈希代码,如上图中的“A”“µ”对象


这是显而易见的,因为可能的不同对象的数量通常大于可能的哈希代码的数量(2^32)。

您的实际问题是什么?
Unequal objects must have different hash codes – WRONG!
Objects with the same hash code must be equal – WRONG!