Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
A hash of hashes

Posted by John_Moylan (jfp), 28 January 2003
Morning all.

New to Java, and still trying to figure a few things.

I'm from a Perl background, probably the worst background to come to Java from and tend to build structures similar to the sample below.

my %catagories = (
                 'Audio' =>      {
                                         'image'    => 'pathA',
                                         'url'      => 'urlA',
                                         'text'     => 'textA',
                                         'template' => 'tplA',
                                         'short'    => 'shortA'
                 'Computers' =>  {
                                             'image'    => 'pathB',
                                             'url'      => 'urlB,
                                             'text'     => 'textB',
                                             'template' => 'tplB',
                                             'short'    => 'shortB'
                 'Food' =>       {
                                         'image'    => 'pathC',
                                         'url'      => 'urlC',
                                         'text'     => 'textC',
                                         'template' => 'tplC',
                                         'short'    => 'shortC'

I would like to replicate this style in Java, probably using hashTables or the like (though some help with an arrayList of arrayLists wouldn't go amiss too as I use arrays of arrays in Perl, but hear that arrayLists are more flexible)
Or am I approaching this all wrong? perhaps I should discard any Perl'y' thoughts of structures.

sorry for the code formatting, its gone a bit strange.

Posted by admin (Graham Ellis), 28 January 2003
There's a rough parallel between Perl's Hashes and HashMaps in Java, and between Perl's lists and ArrayLists in Java.

You'll be setting up your structures using objects, constructors and the like in Java - at first it will seem slow and longwinded but it will get easier with time, and you should find yourself able to maintain the code much more easily once you've put the initial effort in.  In other words - though I have given to a comparison don't try to do a straight conversion because Java is different

You could also use a Hashtable in place of a HashMap, and a Vector in place of an ArrayList - in both cases, these alternatives are recommended only if you want Java 1 compatability or you're writing code to be threadsafe.  A HashSet is something different - it has keys but no values, and you would use if for the type os situation where, in Perl, you set up a has and just put a "1" into the value to make it true.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho