English 中文(简体)
这一说法指的是压倒性 has形和平等吗?
原标题:What does this statement mean about overriding hashcode and equals?

我的发言涉及java等于和hcode。

如果我们将这种Integer物体用于哈希姆普的钥匙,我们就无法可靠地收回相关的价值。

它意味着什么? 它为什么说不可靠? 我写了一个测试方案,它始终行之有效。

public class Test1 {

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

        map.put(1, new Student("john"));        
        map.put(2, new Student("peter"));

        Student s1 = map.get(1);
        Student s2 = map.get(1);
        Student s3 = map.get(2);
        System.out.println("s1:"+s1+" s2:"+s2+" s3:"+s3);

        System.out.println(s1==s2);
        System.out.println(s1==s3);
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s2));
    }   
}

class Student{
    private String name;
    public Student(String name){        
        this.name = name;       
    }

    public String getName(){
        return this.name;
    }   
}
最佳回答

你们被误解! 见whole段:

What would happen if Integer did not override equals() and hashCode()? Nothing, if we never used an Integer as a key in a HashMap or other hash-based collection. However, if we were to use such an Integer object for a key in a HashMap, we would not be able to reliably retrieve the associated value, unless we used the exact same Integer instance in the get() call as we did in the put() call.

rel=“nofollow” http://www.ibm.com/developerworks/java/library/j-jtp05273/index.html

This is saying that the Integer class must have a hashCode() method and equals() in order to work. It does have those methods, so it s fine. It will work.

举例说,不应将提及储存价值的物体加以比较,因为有两个<编码>的Integers,其总分类值可能实际上不同。

Integer x = new Integer(5);
Integer y = new Integer(5);

<代码>x.qualitys(y)始终属实。 x = y 并非总是真实的(但有时可能)。 规则改变了不同价值范围。 除非你完全知道你正在做什么,否则永远不依赖<代码>=。

并请注意,你通过<条码>int论点(原始类型)而不是<条码>Integers(参考物体),但您的藏书一般为<条码>Integer<>。 这意味着,也有一些箱子 to入 equation子!

问题回答

The full context of the quote :

What would happen if Integer did not override equals() and hashCode()? Nothing, if we never used an Integer as a key in a HashMap or other hash-based collection. However, if we were to use such an Integer object for a key in a HashMap, we would not be able to reliably retrieve the associated value, unless we used the exact same Integer instance in the get() call as we did in the put() call.

Java的违约平等测试是一种严格的平等测试(即var1var2,条件是并且只在var1var2上注明同一物体。 但是,如果你使用<代码>Integer作为/Map的关键,你可能希望其所代表的实际编号为关键(而不是Integer. 例本身),这与这一严格的平等测试是不相容的。 因此,<代码>Integer必须优先于hashCode/code>和使这项工作(的确如此)平等<>。

你的问题缺乏一些信息。

您所谈论的是具体的<代码>hashcode()和. Equals(合同。 它们需要遵循某些特性,才能正确,因此可以由依赖这些特性的其他类别使用(Java Collections广泛使用.hashcode(.qualitys()。

http://www.technofundo.com/tech/java/equihash.html” rel=“nofollow”http://www.technofundo.com/tech/java/equihash.html。

“有效 Java”一书也非常好地说明了这些方法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...