这个equals方法传入一个 null 会怎么样?

解决方案 »

  1.   

    F
    对象不同,hashcode就不同,hashcode是利用对象的属性,根据特定的算法计算出来的
      

  2.   

    C,相等的对象一定要有相同的hashcode
      

  3.   

    答案是 A(0) C(a+b) E(a^b)JDK API中对hashCode()的实现要求是
    如果根据 equals(java.lang.Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。 
    如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法 不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。据此要求,A、C、E均符合要求。特别指出的是,根据第二原则,A(0)也符合要求,但在某些场合下效果不好而以。
      

  4.   

    C E
    equals 揭示 a b 可交换
    只有 a + b 和 a^b 满足
      

  5.   

    ace应该都可以吧,equals=>hashcode相同,上面已经说了,加法和异或是可交换的,0是恒定之也可以
    因为不需要反推,所以这三个应该都可以
      

  6.   

    这个题应该选A。hashCode()方法有自己的规则。
    The general contract of hashCode is:    * Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
        * If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
        * It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. 
      

  7.   


    这个题应该选A,C,E。
      

  8.   

    为什么?
    什么叫散列?hashCode用来干什么?难道让这个类的所有的实例的hashCode方法都返回一个相同的值是值得推荐的做法么?
      

  9.   

    哈哈,这样的hashCode方法在逻辑上是没有问题的,但是在实际应用中,特别是在散列中使用时,是不合适的。
    从题目本身来说,主要是考察逻辑问题,所以本题目的A选项是没有错的。
      

  10.   

    争了半天,我们不知道出题人是考我们对于hashcode的深刻理解呢。
    还是只是考语法。
      

  11.   

     * Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. 
        * If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 
        * It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. a  c
      

  12.   

    根据一般约定,如果2个对象 equals 比较为true,那么hashCode也最好(不是必须)相同。
                  如果2个对象 equals 比如为false,那么hashCode也最好(不是必须)不同
    因此对于这个题目,如果从纯语法层面考虑,全部都是可选的
    但是从程序运行效率来看,最好是该hashCode里,a,b值可以互换,而又尽可能做到不同的a,b值返回不同的值。因为C,E最好.
      
    A。return 0;       效率太低
    B. return a;       不满足a,b的互换性
    D. return a-b;     不满足a,b的互换性
    F. return (a < <16)|b; 不满足a,b的互换性另外,比如 a * b  也满足互换性,但是效率稍低,毕竟没加法和位运算快
      

  13.   

    hashcode()在实现使用时,又没有实用价值。
    equals 就可以搞定。
    如果考官问我关于hashcode的问题。我直接让他滚蛋。
      

  14.   


    建议你仔细学习Hashing的原理以及使用。参阅Java API: Hashtable, HashMap or HashSet。
    还有这本书的第七章第一部分。
    “SCJP Sun® Certified Programmer for Java™ 6 Study Guide (Exam 310-065)”
    Kathy Sierra, Bert Bates
    McGraw-Hill 2008