5

这是我第一次使用这个网站,所以如果我没有正确使用它,我深表歉意。请务必让我知道。

无论如何,我有一个 Account 对象,它接受 2 个字符串......一个 acctName 和 lastName(代码如下)。

我想将此对象插入到哈希表中,键为 acctName,并且我想使用多项式来减少冲突。我听说我必须重写 hashCode() 和 equal 方法。我相信我已经正确地覆盖了,但我不确定它是否正确,因为它似乎没有被调用。有人可以告诉我我是否做对了(在正确的位置覆盖并正确添加)并向我解释如何在添加后打印?

感谢并期待将来为社区做出贡献!

类--->账户

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 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);

 }

在帐户对象内使用 GETTER 进行编辑

   public String testGetter()
  {

     return acctName.hashCode() + lastName.hashCode();
    }
4

5 回答 5

1

关键字段的 HashCode 用于散列。您正在使用字符串作为键,并为您的自定义类实现哈希码。这就是为什么它没有被调用。

于 2012-12-08T03:02:18.203 回答
0

如果您没有任何重复的帐户名称,则可以使用每个帐户的名称作为映射键。

未调用您的hashCode()方法,因为您没有使用 Account 对象作为键:您使用的是字符串。

以下是使用其 accountName 将帐户放入地图的方法:

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);

请注意,您必须实现 Account.getAccntName(),如下所示:

public String getAccttName() {
    return acctName;
}

顺便说一句,看起来您在覆盖hashCode()equals()方面做得很好。

而且...欢迎来到 StackOverflow。

于 2012-12-08T03:22:40.587 回答
0

我已经尝试过folwinf。但是编译器没有显示任何错误!!!!

包装测试;

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);
}

}

于 2012-12-08T03:30:17.413 回答
0

您正在做一些与您想的不一样的事情。您正在使用字符串作为与您的对象无关的键。与您想用作帐户名称的字符串相同(实际上不是因为您的大写)并不重要,但是您还通过使用相同的键两次覆盖 Hashmap 中的对象(您是 hasmap将不再存储帐户,仅帐户3)。

在我看来,您想使用 Set 而不是 Map。

于 2012-12-08T03:10:33.380 回答
0

hashmap 实现 hashCode() 实现来计算对象的 hashcode 以用作 hashmap 中的键。

从您的代码中,您似乎想将用户名映射到相应的帐户。在这种情况下,hashCode() 和 equals() 覆盖是没有用的。

注意:对象的 hashCode() 仅在对象用作键时使用。在您的情况下, java.lang.String 类的 hashCode() 被用于插入 Hash 映射。

  • hashCode 和 equals 的条件

    1. 每当在 Java 应用程序执行期间对同一个对象多次调用它时,hashCode 方法必须始终返回相同的整数,前提是没有修改对象上的 equals 比较中使用的信息。该整数不需要从应用程序的一次执行到同一应用程序的另一次执行保持一致。

    2. 如果两个对象根据 equals(Object) 方法相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。

    3. 每当你重写 equals() 方法时,你也需要重写 hashCode() 方法。

于 2012-12-08T06:28:05.703 回答