简体   繁体   中英

Hashtable overriding

This is my first time using this website so I apologize if I am not using it correctly. Please do let me know.

Anyway I have an Account object that takes in 2 strings... An acctName, and lastName (Code is below).

I want to insert this object into a hash table with the key being the acctName and I would like to use polynomials for reducing collision. I have heard that I must override hashCode() and equal method. I believe I have overridden correctly but I am not sure it is correct because it seems to not be called. Can someone tell me if I am doing this right (Overriding in the right location and adding correctly) and explain to me how to print after an add?

Thanks and looking forward to contributing to the community in the future!

Class---> Account

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

Class----> Driver

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

 }

EDIT WITH GETTER INSIDE Account Object

   public String testGetter()
  {

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

HashCode of the key field is used for hashing. You are using string as key, and implementing hashcode for you custom class. that's why it is been not called.

You're doing a couple of things that don't do what you think. You are using a String as a key that is unrelated to your object. It doesn't matter that is the same string you want to use as account name (which actually isn't because of your capitalization), but you're also overwriting objects in the Hashmap by using the same key twice (you're hasmap won't store account anymore, only account3).

It looks to me that you want to use a Set instead of a Map.

If you don't have any duplicate account names, then it's perfectly fine to use the name of each Account as the map key.

Your hashCode() method is not called because you're not using Account objects as the key: you're using Strings.

Here is how you would put an account into the map using its 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);

Note that you'll have to implement Account.getAccntName(), which would look like this:

public String getAccttName() {
    return acctName;
}

By the way, it looks like you've done a good job overriding hashCode() and equals() .

And... welcome to StackOverflow.

I have tried the follwinf. But the compiler don't show anything wrong!!!!

package test;

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

}

The hashmap implements the hashCode() implementation to calculate the hashcode from the object to be used as key in the hashmap.

From your code, it seems that you want to map user names to the corresponding accounts. In such a case the hashCode() and equals() overriding is of no use.

Note : hashCode() for an object will only be used if the object is used as a key. In your case, hashCode() of java.lang.String class is being used to insert into Hash map.

  • Conditions for hashCode and equals

    1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

    2. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

    3. Whenever you override the equals() method, youneed to override the hashCode() method as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM