1

I am new to Java and i am currently rewriting a Perl code in Java. In Perl i have a Hash of Hash like

$hashref->{index1}->{index2}->{index3} = $value;

In this i store the index1 , index2 , and index3 in 3 separate hashes along with above hash. Also Index1 and index2 are of type long and index3 is String.

Now i want to rewrite the similar stuff in Java. So i came up with 2 ways

1)HashMap<Long, HashMap<Long ,HashMap<String,String> >>
2)HashMap<String, String> //In this approach i concatenate index1,index2 and index3 using _ and make a single string

I want to know which way will be efficient as max of 100 keys for index1, 300 keys for index2 and 700 keys for index3 is possible.

DVK
  • 126,886
  • 32
  • 213
  • 327
Raghuram
  • 3,937
  • 2
  • 19
  • 25
  • 1
    possible duplicate of [Java equivalent of Perl's hash](http://stackoverflow.com/questions/4769111/java-equivalent-of-perls-hash) – DVK Feb 15 '12 at 03:43
  • Please note that your first code snipped is definitely NOT compilable Perl code :) – DVK Feb 15 '12 at 03:44
  • refer DVK link... you will get what you want... – Fahim Parkar Feb 15 '12 at 03:45
  • Actually, upon re-reading, I think he meant to ask a SLIGHTLY different question - should a 3level HashMap be used, or 1-level. I'll answer that. – DVK Feb 15 '12 at 03:51
  • The difference in efficiency will be fairly minimal; it depends on your access pattern(s). Nested hashes makes more sense, though, particularly if you need direct access (i.e., without looping over keys to find matches) to particular sub-keys. – Dave Newton Feb 15 '12 at 03:58

1 Answers1

3

First, let's discuss #2

  • It doesn't work 100% of the time due to possible ambiguity if the index strings can contain the separator character ("_"):

    E.g. consider 3 sets of indexes: ("12", "13_13" and "14") and ("12_13", "13" and "14"). They both produce the same concatenated key.

  • It doesn't allow straightforward processing of the 2-d and 3-d level hashes as data structures easily.

    E.g. you can not easily do Java equivalent of "keys %{$hash->{index1}}" - find all the keys for a hash of second level stored under index1. It's doable, but much harder. Or "delete $hash->{index1}".

If these 2 considerations don't bother you, using a concatenated key for 1-level HashMap is fine.

If they do, you need to do #1 - a fairly robust implementation was posted on SO previously: " Java equivalent of Perl's hash " . Please note that such implementation is NOT trivial, and therefore your alternate approach of concatenated 1 level of indexes is a very good alternative.

Community
  • 1
  • 1
DVK
  • 126,886
  • 32
  • 213
  • 327
  • I suspect that option 2 also could have problems if two maps with the same entries had different iteration order. – Louis Wasserman Feb 15 '12 at 03:53
  • @LouisWasserman - if he wants a Perl hash equivalent, I don't think he cares about iteration order at all. Good point though – DVK Feb 15 '12 at 03:56
  • I wouldn't say *much* harder, you'd need to iterate over all the keys and match a given pattern; easy enough if you use different chars between the keys. I'd still use nested hashes, though. – Dave Newton Feb 15 '12 at 03:57
  • @DVK ...I went through Java Equivalent of Perl's Hash and to some extent i am clear with usage. Also please comment which will be faster/efficient my first approach or the second approach? – Raghuram Feb 15 '12 at 04:00
  • @Raghuram faster what? Faster to execute or faster from coding perspective? – DVK Feb 15 '12 at 04:04
  • @Raghuram - depends on your data and access pattrens. Why don't you try both methods and benchmark? Most likely, both are relatively similar performance wise, with 1-level being marginally faster (less HashMap mechanics to deal with) – DVK Feb 15 '12 at 04:30