2

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!

eboix
  • 5,113
  • 1
  • 27
  • 38
DarthestVader
  • 117
  • 1
  • 9

5 Answers5

9

In Java, we use objects. We would have a Person object, with a name property of type String, and a savingsAccount of type Account. This Account object would have a value property, of type BigDecimal.

Java is an OO language. It's not Perl. You should use Java idioms in Java, and not Perl idioms.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • the other day I saw the code of some of our former Perl programmers: `LinkedHashMap>>>` – Bozho Jan 16 '12 at 22:56
5

You can have a Map<Map<..>>, where you'll be able to call map.get("key1").get("key2")

But note that Java is a statically-typed, object-oriented language. So you'd better creat classes: Person, SavingsAccount, and a Person has a field private SavingsAccount savingsAcount. Then you'll be able to do a compile-time safe: map.get("John").getSavingsAccount()

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks for your response. I think the nested map way of doing it is most similar to what I'm used to in Perl. Is it less efficient than using the object approach? – DarthestVader Jan 16 '12 at 23:54
  • If you want to quickly retrieve then map is the fastest while the object approach might need you to use a list which again is a touch expensive if you have to loop through large data. – Liam Jan 17 '12 at 00:11
  • 1
    It's less efficient, and goes against the philosophy of the language. Your code will be considered amateurish if you do that in Java. It will also be unreadable and hard to maintain, and your colleagues will want to shoot you :-) Learn the Java way of doing things, or keep using Perl. – JB Nizet Jan 17 '12 at 00:11
1

You could create a class that represents the key with proper implementation for equals and hashCode methods:

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

I am not sure why the question was down-voted but to answer your question, in Java you can use nested Maps to achieve the same.

Liam
  • 2,837
  • 23
  • 36
0

For instance, you can have a

HashMap<String, HashMap<String, BigDecimal>>. 

I would not call it OOD, but you can.

It would probably be a bit more readable if you had a class for representing Person, a class to represent PersonalAccounts, which has Account instances as attributes, one for each account type (I assume those would be very few, otherwise a list would be better).

Then a single

HashMap<Person, PersonalAccounts> 

is enough, if you want to use an HashMap.

Actually you don't even need a map if an instance of PersonalAccounts is an attribute of a Person.

Savino Sguera
  • 3,522
  • 21
  • 20