I'm trying to understand the following code, where acct1
and acct2
are objects that represent "bank accounts".
HashMap map = new HashMap();
map.put(acct1.hashCode(), acct1);
map.put(acct2.hashCode(), acct2);
for (Object o : map.values())
{
System.out.println(o);
}
for (Object o : map.keySet())
{
System.out.println(o);
}
According to the BankAccount
API, the method hashCode()
returns an int
, which is supposed to be a hash of the account.
So doesn't that mean that the set created by keySet()
contains integers? And if so, why is the second for-each
iterator declaring its members as Object
. When I tried to switch Object
to int
, I got a compiler error.