Hi,
I'm trying to learn HashMaps using objects. The string currently returns null or shows an @923e30 error.
Please advise. Account class and HashMapDemo class
I'm trying to learn HashMaps using objects. The string currently returns null or shows an @923e30 error.
Please advise. Account class and HashMapDemo class
Code:
public class Account{
String customerAccount;
public Account(String customerAccount){ //Constructor
this.customerAccount = customerAccount;
}
public String toString(String customerAccount) {
return ("Customer :" + customerAccount);
}
}
Code:
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
HashMap hm = new HashMap();
Account anAcct1 = new Account("123");
Account anAcct2 = new Account("456");
hm.put("100", anAcct1);
hm.put("200", anAcct2);
Set set = hm.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey() + " : " + me.getValue() );
}
System.out.println("Account number : " + hm.get(anAcct1));
System.out.println(hm.get("100").toString()); // Function toString()
}
}
Comment