Hashtables in Java

Status
Not open for further replies.

asiufy

Ars Tribunus Militum
1,585
Folks,

Being a Perl hacker, I'm having a bit of a hard time figuring out how to do some things in Java...

In Perl, I constantly use hashes of hashes to store lots of data in memory, in key-value pairs.

How do I do the same in Java? I kind of figured out how to add a Hashtable to another Hashtable, but is there any way to easily reference/look up a single item in the main Hashtable, sort of how you can use the -> operator in Perl?

Thanks.
 

wb

Ars Legatus Legionis
25,208
Subscriptor
code:<HR>
Code:
Hashtable hash = new Hashtable();<P>// storing items in Hashtable
hash.put( "one", new Integer(1) );
hash.put( "two", new Integer(2) );
hash.put( "three", new Integer(3) );<P>
// retrieving items from Hashtable
Integer i = (Integer) hash.get("one");
System.out.println( "one = " + i.intValue() );<P>i = (Integer) hash.get("two");
System.out.println( "two = " + i.intValue() );<P>i = (Integer) hash.get("three");
System.out.println( "three = " + i.intValue() );
 

Tychom

Ars Tribunus Militum
2,391
Subscriptor
heh, I was surreptitiously prompting towards a JDK1.2 compliant stance (and the ability to integrate with other JFC classses/interfaces)
As of the Java 2 platform v1.2, this class has been retrofitted to implement Map, so that it becomes a part of Java's collection framework.

Which would seem good practise...
But yeah, roll your own syncronicity.
 

asiufy

Ars Tribunus Militum
1,585
Originally posted by wb:
Quote: "
Hashtable hash = new Hashtable();

// storing items in Hashtable
hash.put( "one", new Integer(1) );
hash.put( "two", new Integer(2) );
hash.put( "three", new Integer(3) );


// retrieving items from Hashtable
Integer i = (Integer) hash.get("one");
System.out.println( "one = " + i.intValue() );

i = (Integer) hash.get("two");
System.out.println( "two = " + i.intValue() );

i = (Integer) hash.get("three");
System.out.println( "three = " + i.intValue() );
"

Sure, I got this part. Now put a Hashtable inside a Hashtable, like this:

<code>
$hash{id} = { key => 1, value => A }
</code>

And then...

<code>
print $hash{id}{1};
</code>

to print "A".

Basically, what I want to know is how to get easy access to nested Hashtables in Java.
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Sure, I got this part. Now put a Hashtable inside a Hashtable, like this:


$hash{id} = { key => 1, value => A }


And then...


print $hash{id}{1};


to print "A".

Basically, what I want to know is how to get easy access to nested Hashtables in Java.

code:<HR>
Code:
Hashtable t = (Hashtable)hash.get(id);
String value = (String) t.get("1"); // assumes you have a String key type, 
                                    // and String value type
System.out.println( value );

BTW I recently wrote a perl program that did something very similar to this (using nested hashtables), and was considering rewriting it in Java so that all the code for the project would be in the same language. However, the parsing part is so much easier to to in Perl I just kept it the way it was.
 

asiufy

Ars Tribunus Militum
1,585
Originally posted by gubmo:
Why do you want to hash them? DO you have a need for the container tables to be hashed? If not, use a Vector or array.

gubmo

That's the thing, in Perl, hashes seem to be the best option, but I believe I could use something else in Java...

I'll be storing information about albums on these hashes. So I can have a key with an album ID, and as value, another has, with the information about that album.
 

asiufy

Ars Tribunus Militum
1,585
Originally posted by futang:
You've got to Javaize your problem. Create a class for albums and store instances of it in the hash.

I haven't tried this yet, but I will, but:

I am now using Castor to bind the XML data to Java objects. So I'm left with a Records object that has all my data, in several different String fields (Band, Album, Year, etc).

I don't know if that's what Castor is doing, but I suppose it is creating a new instance of Records for each <record></record> that gets parsed. If that is true, how do I randomly access the data in those Records objects? Say, how do I list all items with Records.year = "1990" ??
 
Status
Not open for further replies.