In Perl if I want to have a multi-layered hash, I would write:
$hash_ref->{'key1'}->{'key2'}='value';
Where 'key1'
might be a person's name, 'key2'
might be "Savings Account" (vs. "Checking Account") and 'value'
might be the amount of money in the account.
Is there an equivalent to this in Java, i.e. access values via hash references? What's the syntax like for this? Any examples or other resource references would be greatly appreciated. Thanks!
You can have a
Map<Map<..>>
, where you'll be able to callmap.get("key1").get("key2")
But note that Java is a statically-typed, object-oriented language. So you'd better creat classes:
Person
,SavingsAccount
, and aPerson
has a fieldprivate SavingsAccount savingsAcount
. Then you'll be able to do a compile-time safe:map.get("John").getSavingsAccount()