简体   繁体   English

为什么StringBuffer / StringBuilder不覆盖equals或hashCode?

[英]Why does StringBuffer/StringBuilder not override equals or hashCode?

Why does StringBuffer/StringBuilder does not override the equals() , hashcode() methods from object? 为什么StringBuffer / StringBuilder不覆盖对象的equals()hashcode()方法?

Please suggest me clear picture that helps the understand the problem... 请为我建议清晰的图片,以帮助理解问题...

Because StringBuffer is mutable, and its primary use is for constructing strings. 因为StringBuffer是可变的,所以它的主要用途是构造字符串。 If you want to compare content, call StringBuffer#toString() and compare the returned value. 如果要比较内容,请调用StringBuffer#toString()并比较返回的值。

It is not generally useful to override hashCode() for mutable objects, since modifying such an object that is used as a key in a HashMap could cause the stored value to be "lost." 通常,为可变对象覆盖hashCode()并没有什么用,因为修改在HashMap用作键的对象可能会导致存储的值“丢失”。

Actually behind this everything depends upon the hashcode code value. 实际上,这一切都取决于哈希码值。 To understand this concept let's take an example : 为了理解这个概念,让我们举个例子:

String str1 = new String("sunil");
String str2 = new String("sunil");

HashMap hm = new HashMap()
hm.put(str1,"hello");
hm.put(str2,"bye");

final hm: 最后的hm:

hm = { sunil=bye }

In above code, str1 and str2 are two different String objects. 在上面的代码中,str1和str2是两个不同的String对象。 Should they be added to the HashMap separately? 是否应该将它们分别添加到HashMap? The answer is NO . 答案是否定的 This is because before inserting/putting a value in HashMap, it internally checks and compares the hashCode values of str1 , str2 . 这是因为在将值插入/放入HashMap之前,它会在内部检查并比较str1str2的hashCode值。 Both return the same hashcode value because the String class overrides equals() and hashcode() methods. 两者都返回相同的哈希码值,因为String类重写equals()和hashcode()方法。 So upon executing hm.put(str2,"bye"); 因此,在执行hm.put(str2,"bye"); first key will get overriden with the new value. 第一个键将被新值覆盖。 Now try this : 现在尝试:

StringBuilder sb1 = new StringBuilder("sunil");
StringBuilder sb2 = new StringBuilder("sunil");

HashMap hm = new HashMap()
hm.put(sb1,"hello");//sb1 and sb2 will return different HashCode 
hm.put(sb2,"bye");// StringBuffer/StringBuilder does not override hashCode/equals methods

final hm: 最后的hm:

{sunil=hello, sunil=bye}

Both value will be added in hashMap because sb1 and sb2 both returns different hashcode. 这两个值都将添加到hashMap中,因为sb1和sb2都返回不同的哈希码。 StringBuilder/ StringBuffer does not override equals() and hashCode() method. StringBuilder / StringBuffer不会覆盖equals()和hashCode()方法。

Sun Microsystem wanted the programmer to allow adding 2 different String kind of Values in Hashtable or any other Hash Collections likes (HashSet,HashMap…),that's the reason hashCode() and equals() were not overridden intentionally in StringBuffer,StringBuilder class. Sun Microsystem希望程序员允许在Hashtable或任何其他Hash集合中添加2种不同的String类型的值,例如(HashSet,HashMap…),这就是未在StringBuffer,StringBuilder类中故意覆盖hashCode()和equals()的原因。

Because StringBuffer is mutable . 因为StringBuffer是可变的 With example Try This:) 以示例尝试一下:)

package test;

import java.util.HashMap;

public class CheckHashcodeEquals {

    public static void main(String[] args) {

        /*
         * String class override equals() and hashcode() method thats way
         * override value of HashMap
         */
        String s1 = new String("Arya");
        String s2 = new String("Arya");
        HashMap hm = new HashMap<>();
        hm.put(s1, "A1");
        hm.put(s2, "A2");
        System.out.println(hm); /* Output: {Arya=A2} */

        /*
         * String class does not override equals() and hashcode() method thats
         * way insert duplicate value
         */
        StringBuffer sb1 = new StringBuffer("Arya");
        StringBuffer sb2 = new StringBuffer("Arya");
        HashMap hm2 = new HashMap<>();
        hm2.put(sb1, "A1");
        hm2.put(sb2, "A2");
        System.out.println(hm2); /* Output: {Arya=A2, Arya=A1} */
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 java 类 WeakReference 不会覆盖 hashcode 和 equals - why java class WeakReference does not override hashcode and equals StringBuilder 实现 Comparable 但不覆盖 equals - StringBuilder implements Comparable but does not override equals 为什么ArrayDeque不重写equals()和hashCode()? - Why doesn't ArrayDeque override equals() and hashCode()? 为什么更改 StringBuilder 会更改其 hashCode? - Why does changing StringBuilder change its hashCode? 为什么new StringBuffer(new StringBuilder(“ foo”))可以工作? - Why new StringBuffer(new StringBuilder(“foo”)) does work? 如果我覆盖Java中的'equals'方法,为什么需要重写hashcode? - Why is there a need to override hashcode if I override the 'equals' method in Java? 当我覆盖equals()方法时,为什么要覆盖hashCode()? - Why should I override hashCode() when I override equals() method? 为什么StringBuilder比StringBuffer慢? - Why is StringBuilder slower than StringBuffer? “StringBuffer 是同步的(或线程安全的)而 StringBuilder 不是”,为什么这会使 StringBuffer 方法变慢? - “StringBuffer is synchronized (or thread-safe) and StringBuilder is not”, why does this make StringBuffer methods slower? 如果存在hashCode(),为什么Java需要equals()? - Why does Java need equals() if there is hashCode()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM