Java 哈希表重写
这是我第一次使用这个网站,所以如果我没有正确使用它,我深表歉意。请务必让我知道 无论如何,我有一个帐户对象,它包含两个字符串。。。帐户名和姓氏(代码如下) 我想将这个对象插入一个哈希表,其中键是acctName,我想使用多项式来减少冲突。我听说我必须重写hashCode()和equal方法。我相信我已经正确地重写了,但我不确定它是否正确,因为它似乎没有被调用。有人能告诉我我是否做得对(在正确的位置覆盖并正确添加),并向我解释如何在添加后打印吗 感谢并期待在未来为社区做出贡献 类别--->账户Java 哈希表重写,java,android,Java,Android,这是我第一次使用这个网站,所以如果我没有正确使用它,我深表歉意。请务必让我知道 无论如何,我有一个帐户对象,它包含两个字符串。。。帐户名和姓氏(代码如下) 我想将这个对象插入一个哈希表,其中键是acctName,我想使用多项式来减少冲突。我听说我必须重写hashCode()和equal方法。我相信我已经正确地重写了,但我不确定它是否正确,因为它似乎没有被调用。有人能告诉我我是否做得对(在正确的位置覆盖并正确添加),并向我解释如何在添加后打印吗 感谢并期待在未来为社区做出贡献 类别--->账户 p
public class Account
{
private String acctName;
private String lastName;
public Account(String acctName, String lastName)
{
this.acctName= acctName;
this.lastName= lastName
}
@Override
public int hashCode() {
return acctName.hashCode() + lastName.hashCode();
}
@Override
public boolean equals (Object otherObject) {
if (!(otherObject instanceof Account)) {
return false;
}
if (otherObject == this) {
return true;
}
Account accountHolder = (Account) otherObject;
return acctName.equals(accountHolder.acctName) && lastName.equals(accountHolder.lastName);
}
public String testGetter()
{
return acctName.hashCode() + lastName.hashCode();
}
类-->驱动程序
public void insertInto()
{
Hashtable<String,Account> hash=new Hashtable<String,HoldInformation>();
Account account= new Account ("Deposit", "Jones");
Account account2= new Account ("Withdraw", "Smith");
hash.put ("deposit", account);
hash.put ("Withdraw", account2);
}
密钥字段的哈希代码用于哈希。您使用字符串作为键,并为自定义类实现hashcode。这就是它没有被调用的原因。您正在做的一些事情并没有按照您的想法做。您将字符串用作与对象无关的键。您想用作帐户名的字符串是否相同并不重要(这实际上不是因为您的大写),但您也会使用相同的键两次覆盖Hashmap中的对象(您知道hasmap不再存储account,只存储account3)
在我看来,您希望使用集合而不是映射。如果您没有任何重复的帐户名,则可以使用每个帐户的名称作为映射键 没有调用hashCode()方法,因为您没有使用帐户对象作为键:您使用的是字符串 以下是如何使用帐户名将帐户放入地图:
Account accountOne = new Account("checking", "Smith");
Account accountTwo = new Account("saving", "Jones");
Map<String, Account> accountMap = new HashMap<String, Account>();
accountMap.put(accountOne.getAcctName(), accountOne);
accountMap.put(accountTwo.getAcctName(), accountTwo);
顺便说一下,看起来您在重写hashCode()和equals()方面做得很好
而且。。。欢迎来到StackOverflow。我已经尝试了以下方法。但是编译器没有显示任何错误 包装试验
import java.util.Hashtable;
public class Account {
private String acctName;
private String lastName;
public Account(String acctName, String lastName)
{
this.acctName= acctName;
this.lastName= lastName;
}
@Override
public int hashCode() {
return acctName.hashCode() + lastName.hashCode();
}
@Override
public boolean equals (Object otherObject) {
if (!(otherObject instanceof Account)) {
return false;
}
if (otherObject == this) {
return true;
}
Account accountHolder = (Account) otherObject;
return acctName.equals(accountHolder.acctName) && lastName.equals(accountHolder.lastName);
}
public String testGEtter()
{
return lastName+","+acctName;
}
public static void mian(String args[])
{
Hashtable<String , Account> table = new Hashtable<>();
Account acc= new Account("test1", "test2");
table.put(acc.testGEtter(), acc);
}
import java.util.Hashtable;
公共类帐户{
私有字符串帐户名;
私有字符串lastName;
公共帐户(字符串acctName,字符串lastName)
{
this.acctName=acctName;
this.lastName=lastName;
}
@凌驾
公共int hashCode(){
返回acctName.hashCode()+lastName.hashCode();
}
@凌驾
公共布尔等于(对象其他对象){
如果(!(帐户的其他对象实例)){
返回false;
}
if(otherObject==此){
返回true;
}
账户持有人=(账户)其他对象;
返回acctName.equals(accountHolder.acctName)和&lastName.equals(accountHolder.lastName);
}
公共字符串testGEtter()
{
返回lastName+“,”+acctName;
}
公共静态void mian(字符串参数[])
{
Hashtable=新的Hashtable();
账户acc=新账户(“测试1”、“测试2”);
表.put(根据testGEtter(),根据);
}
}hashmap实现hashCode()实现,从要用作hashmap中键的对象计算hashCode 从您的代码来看,似乎您希望将用户名映射到相应的帐户。在这种情况下,hashCode()和equals()重写是没有用的 注意:仅当对象用作键时,才会使用对象的hashCode()。 在您的例子中,java.lang.String类的hashCode()用于插入到哈希映射中
- hashCode和equals的条件
- 在Java应用程序的执行过程中,每当在同一对象上多次调用hashCode方法时,只要没有修改对象上的equals比较中使用的信息,hashCode方法必须始终返回相同的整数。从应用程序的一次执行到同一应用程序的另一次执行,该整数不必保持一致
- 如果根据equals(Object)方法两个对象相等,那么对两个对象中的每一个调用hashCode方法必须产生相同的整数结果
- 无论何时重写equals()方法,都需要重写hashCode()方法
import java.util.Hashtable;
public class Account {
private String acctName;
private String lastName;
public Account(String acctName, String lastName)
{
this.acctName= acctName;
this.lastName= lastName;
}
@Override
public int hashCode() {
return acctName.hashCode() + lastName.hashCode();
}
@Override
public boolean equals (Object otherObject) {
if (!(otherObject instanceof Account)) {
return false;
}
if (otherObject == this) {
return true;
}
Account accountHolder = (Account) otherObject;
return acctName.equals(accountHolder.acctName) && lastName.equals(accountHolder.lastName);
}
public String testGEtter()
{
return lastName+","+acctName;
}
public static void mian(String args[])
{
Hashtable<String , Account> table = new Hashtable<>();
Account acc= new Account("test1", "test2");
table.put(acc.testGEtter(), acc);
}