HashSet添加两个对象,它们为equals()返回true并且在Java中具有相同的哈希码
所述frequencySet()
被计算的每个字符的频率String
在一个Integer[]
包裹成Counter
具有被覆盖的equals和哈希码类。此方法应该仅返回集合中的唯一频率,但会添加两个Counter
对象。
从打印语句可以看出:hashcode()
返回相等的值,并且equals()
返回true
。
怎么了?
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String[] a = new String[2];
a[0]="tan";
a[1]="nat";
Set<Counter> s = frequencySet(a);
System.out.println(s.size()); // prints
System.out.println(getFreq(a[0]).equals(getFreq(a[1])) + ":" + getFreq(a[0]).hashcode() + ":" + getFreq(a[1]).hashcode() );
}
public static Set<Counter> frequencySet(String[] strs) {
Set<Counter> set = new HashSet<>();
for(String s: strs){
Counter counter = getFreq(s);
set.add(counter);
//System.out.println(s + " : " + counter.hashcode() + " : " + Arrays.toString(counter.arr) );
}
return set;
}
private static Counter getFreq(String s){
Integer[] frequency = new Integer[26];
for(char c : s.toCharArray()){
Integer a = frequency[c-'a'];
if(frequency[c-'a']==null){
frequency[c-'a']=1;
}
else{
a++;
frequency[c-'a']=a; //++frequency[c-'a'];
}
}
//System
return new Counter(frequency);
}
}
class Counter{
Integer[] arr;
public Counter(Integer[] arr){
this.arr=arr;
}
public int hashcode(){
//return Arrays.deepHashCode((Integer[])arr);
int hashcode=31;
for(int i=0;i<arr.length;i++){
if(arr[i]==null)
hashcode+=i;
else
hashcode+=31*arr[i];
}
return hashcode;
}
public boolean equals(Object o){
//return Arrays.deepEquals(arr,(Integer[])o);
Counter c = (Counter)o;
Integer[] other = c.arr;
for(int i=0;i<26;i++){
if((arr[i]==null && other[i]!=null) || (arr[i]!=null && other[i]==null)){
return false;
}
else if(arr[i]!=null && other[i]!=null && arr[i]!=other[i]){
return false;
}
}
return true;
}
}
输出:
2
true:417:417
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String[] a = new String[2];
a[0]="tan";
a[1]="nat";
Set<Counter> s = frequencySet(a);
System.out.println(s.size()); // prints
System.out.println(getFreq(a[0]).equals(getFreq(a[1])) + ":" + getFreq(a[0]).hashcode() + ":" + getFreq(a[1]).hashcode() );
}
public static Set<Counter> frequencySet(String[] strs) {
Set<Counter> set = new HashSet<>();
for(String s: strs){
Counter counter = getFreq(s);
set.add(counter);
//System.out.println(s + " : " + counter.hashcode() + " : " + Arrays.toString(counter.arr) );
}
return set;
}
private static Counter getFreq(String s){
Integer[] frequency = new Integer[26];
for(char c : s.toCharArray()){
Integer a = frequency[c-'a'];
if(frequency[c-'a']==null){
frequency[c-'a']=1;
}
else{
a++;
frequency[c-'a']=a; //++frequency[c-'a'];
}
}
//System
return new Counter(frequency);
}
}
class Counter{
Integer[] arr;
public Counter(Integer[] arr){
this.arr=arr;
}
public int hashcode(){
//return Arrays.deepHashCode((Integer[])arr);
int hashcode=31;
for(int i=0;i<arr.length;i++){
if(arr[i]==null)
hashcode+=i;
else
hashcode+=31*arr[i];
}
return hashcode;
}
public boolean equals(Object o){
//return Arrays.deepEquals(arr,(Integer[])o);
Counter c = (Counter)o;
Integer[] other = c.arr;
for(int i=0;i<26;i++){
if((arr[i]==null && other[i]!=null) || (arr[i]!=null && other[i]==null)){
return false;
}
else if(arr[i]!=null && other[i]!=null && arr[i]!=other[i]){
return false;
}
}
return true;
}
}
回答
始终使用@Override
. 你已经实现了 method public int hashcode()
。那不是你认为的那样。你要public int hashCode()
。注意大写 C. 添加@Override
注释,编译器会出错(先尝试:@Override public int hashcode()...
,然后编译它,注意错误,通过使hashCode
.
请注意,您的 hashCode impl 效率极低且很奇怪。你听说过一些关于素数的事情,但没有正确地实现它。正确的方法是将您编写的 hashcode乘以一个素数(如果必须),而不是下一个元素值。
更一般地说,Arrays.deepHashCode
在这里就好了。我想这就是发生的事情:
- 您使用 deepHashCode 编写了哈希码实现。
- 该代码不起作用。
- 您(错误地)将 deepHashCode 归咎于罪魁祸首,并编写了自己的哈希器。
- 它仍然不起作用 - 合乎逻辑,因为这不是问题(用小写 c 编写哈希码是问题所在)。
恢复您的“修复” - 使用 deepHashCode 的此代码更易于阅读且更高效。
THE END
二维码