hashCode()
和equals()
方法没有被覆盖StringBuffer
,StringBuilder
我在下面尝试这段代码..
//StringBuffer does not override equals & hashCode
//StringBuffer s = new StringBuffer("saral");
//StringBuffer s1 = new StringBuffer("saral");
StringBuilder s = new StringBuilder("saral");
StringBuilder s1 = new StringBuilder("saral");
//String s = new String("saral");
//String s1 = new String("saral");
HashSet set=new HashSet();
set.add(s);
set.add(s1);
set.add(null);
System.out.println("There are "+set.size()+" elements in the set.");
结果我得到的结果在我使用的两种情况下都是 3,StringBuffer
或者StringBuilder
在 string 的情况下是 2,因为 string 已经覆盖了hashCode()
andequals()
方法,请告知。