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
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.

Code:
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.
Thanks
jfp


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 www.opentalk.org.uk 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho