HashMap...这里得到空值,我必须重载 hashcode() 以便我各自的对象
HashMap... here getting null, i've to overload hashcode() inorder to my respective object
class Student2{
int age;
String name;
Student2(int age,String name)
{
this.age=age;
this.name=name;
}
/*
public int hashCode(){
return age;
}
*/
public boolean equals(Object o){
Student2 s2=(Student2)o;
return (this.age==s2.age && this.name==s2.name);
}
}
public class HashMapDemo{
public static void main(String a[])
{ Map m=new HashMap();
m.put(new Student2(10,"sameer"), 1);
m.put(new Student2(11,"pagal"), 2);
m.put(new Student2(12,"ullu"), 3);
m.put(new Student2(13,"lullu"), 5);
System.out.println(m.get(new Student2(11,"pagal")));
}
}
HashMap...这里得到 null,我必须重载 hashcode() 以便我各自的对象。
我要在 hashCode 中添加什么。
我怀疑 hashCode 方法是否必须覆盖 hashCode 方法。
在您的 "System.out.println" 中,您要求获得您此时正在创建的元素。该元素与您正在搜索的元素具有相同的属性,但它是一个新元素。因此他找不到它。
您必须要求一个已经插入的对象。但是为此,您必须指定您想要的那个。
修改你的代码,像这样:
Student2 myStud = new Student2(11,"pagal");
m.put(myStud, 2);
...
System.out.println(m.get(myStud));
如果您不覆盖 hashcode()
方法,那么将使用默认的 Object.hashcode()
方法,即使对象是具有相同字段值的相同类型。
如果您取消注释您的哈希码方法,您应该会得到您想要的结果这次。但是,您的 hashcode()
和 equals()
方法存在一些缺陷。
在您的 equals 方法中,您正在使用 ==
检查字符串的相等性,而您不应该这样做。请使用 .equals()
来比较字符串。这只会起作用,因为您对字符串进行了硬编码,因此编译器将恰好对两个 "pagal"
s.
使用相同的实例
您的 hashcode()
和 equals()
方法应该检查相同的字段。您的 hashcode()
方法仅使用 age
,但您的 equals 同时使用 age
和 name
。将您的 equals 更改为仅使用 age
或将 hashcode()
更改为同时使用 age
和 name
。
有关如何正确实施 equals()
和 hashcode()
的更多信息,请阅读 Effective Java 一书或查看此 IBM java development link
如果您想将 class 用作密钥,则需要 hashCode 和 equals 方法。让 IDE 生成或使用:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ( ( name == null ) ? 0 : name.hashCode() );
return result;
}
@Override
public boolean equals( Object obj ) {
if( this == obj )
return true;
if( obj == null )
return false;
if( getClass() != obj.getClass() )
return false;
Student2 other = (Student2) obj;
if( age != other.age )
return false;
if( name == null ) {
if( other.name != null )
return false;
} else if( !name.equals( other.name ) )
return false;
return true;
}
当在 HashMap 中用作 key
时,您必须覆盖 Student2
class 中的 hashcode() 和 equals() 方法。
检查文档 here
class Object 定义的 hashCode 方法为不同的对象执行 return 不同的整数。这通常是通过将对象的内部地址转换为整数来实现的,因此您收到 null.
在这种情况下,您必须覆盖 hashcode
。
如果两个对象通过 equal 方法相等,那么它应该具有相同的哈希码。这是哈希映射的工作方式
public int hashCode(){
return age;
}
我认为这对你的情况很有效。
但如果你有很多同龄以下的学生,它会无声地降低性能。因为哈希桶越来越大 sdo on。所以试着放一些有效的哈希码
class Student2{
int age;
String name;
Student2(int age,String name)
{
this.age=age;
this.name=name;
}
/*
public int hashCode(){
return age;
}
*/
public boolean equals(Object o){
Student2 s2=(Student2)o;
return (this.age==s2.age && this.name==s2.name);
}
}
public class HashMapDemo{
public static void main(String a[])
{ Map m=new HashMap();
m.put(new Student2(10,"sameer"), 1);
m.put(new Student2(11,"pagal"), 2);
m.put(new Student2(12,"ullu"), 3);
m.put(new Student2(13,"lullu"), 5);
System.out.println(m.get(new Student2(11,"pagal")));
}
}
HashMap...这里得到 null,我必须重载 hashcode() 以便我各自的对象。 我要在 hashCode 中添加什么。 我怀疑 hashCode 方法是否必须覆盖 hashCode 方法。
在您的 "System.out.println" 中,您要求获得您此时正在创建的元素。该元素与您正在搜索的元素具有相同的属性,但它是一个新元素。因此他找不到它。 您必须要求一个已经插入的对象。但是为此,您必须指定您想要的那个。
修改你的代码,像这样:
Student2 myStud = new Student2(11,"pagal");
m.put(myStud, 2);
...
System.out.println(m.get(myStud));
如果您不覆盖 hashcode()
方法,那么将使用默认的 Object.hashcode()
方法,即使对象是具有相同字段值的相同类型。
如果您取消注释您的哈希码方法,您应该会得到您想要的结果这次。但是,您的 hashcode()
和 equals()
方法存在一些缺陷。
在您的 equals 方法中,您正在使用
==
检查字符串的相等性,而您不应该这样做。请使用.equals()
来比较字符串。这只会起作用,因为您对字符串进行了硬编码,因此编译器将恰好对两个"pagal"
s. 使用相同的实例
您的
hashcode()
和equals()
方法应该检查相同的字段。您的hashcode()
方法仅使用age
,但您的 equals 同时使用age
和name
。将您的 equals 更改为仅使用age
或将hashcode()
更改为同时使用age
和name
。
有关如何正确实施 equals()
和 hashcode()
的更多信息,请阅读 Effective Java 一书或查看此 IBM java development link
如果您想将 class 用作密钥,则需要 hashCode 和 equals 方法。让 IDE 生成或使用:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ( ( name == null ) ? 0 : name.hashCode() );
return result;
}
@Override
public boolean equals( Object obj ) {
if( this == obj )
return true;
if( obj == null )
return false;
if( getClass() != obj.getClass() )
return false;
Student2 other = (Student2) obj;
if( age != other.age )
return false;
if( name == null ) {
if( other.name != null )
return false;
} else if( !name.equals( other.name ) )
return false;
return true;
}
当在 HashMap 中用作 key
时,您必须覆盖 Student2
class 中的 hashcode() 和 equals() 方法。
检查文档 here
class Object 定义的 hashCode 方法为不同的对象执行 return 不同的整数。这通常是通过将对象的内部地址转换为整数来实现的,因此您收到 null.
在这种情况下,您必须覆盖 hashcode
。
如果两个对象通过 equal 方法相等,那么它应该具有相同的哈希码。这是哈希映射的工作方式
public int hashCode(){
return age;
}
我认为这对你的情况很有效。 但如果你有很多同龄以下的学生,它会无声地降低性能。因为哈希桶越来越大 sdo on。所以试着放一些有效的哈希码