File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes 3 Dimensional Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "3 Dimensional Hashtable" Watch "3 Dimensional Hashtable" New topic
Author

3 Dimensional Hashtable

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
Hi,
I�ve been wracking my brain for three days trying to develop a Class to use as a 3 dimensional Hashtable.
Does anybody have one or something close that they�d like to share?
Any logic suggestions?
Thank You in Advance for Your Help,
Lou
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Can you give some sort of example of what you're trying to do? Is it like a Map with two keys maybe? What sort of data are you putting into it? What type of queries would you need to do?


"I'm not back." - Bill Harding, Twister
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
Hi Jim,
The ultimate goal is to load all drop-down window information from a table with a single SQL call to a Hashtable when the application starts. Then when that information is needed it would be accessed via the Hashtable.
Since the table has three key fields to be unique, I've decided that the Hashtable needs three dimensions. I'd like this three-dimensional Hashtable to be available for other uses, hence make it a seperate class.
The Hashtable values should be returned in some sort of container, I'm thinking a simple list, and should be accessed by zero or more keys. For instance a method that receives zero keys as arguments would return all the values in the Hashtable, a method that receives two keys as arguments will return the values for those two keys, etc.
If you have a better approach I'd like to hear it.
Today (no I don't work on Sundays, but this is stuck in my head), I've been kicking around the idea using array[3] as the key to the Hastable. Each occurance of the array would be one key. If this would work then the Dimensioal Hashtable could have as many dimensions as there are occurances in the array.
Thanks,
Lou
[ November 23, 2003: Message edited by: Lou P ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, you could use a normal Hashtable (or preferably, HashMap) using a composite key, e.g.

You can make a similar class for a triple composite key. This can be used in a HashMap so that both (or all three) keys are used to locate an object. This is fairly fast and easy. Except that you want to also be able to seach on just one key, or two out of three keys. That's more difficult, as the CompositeKey2 class above only works if both keys are non-null. You can revise it so it won't throw a NullPointerException, but you can't make it return a hashCode() that would retrieve all the records for which the other key values match. You need all 2/3 keys to make the hashcode. So if you need to be able to search on multiple combinations of key, you may need something like:
3 HashMaps with normal keys (A, B, C)
3 HashMaps with double composite keys (AB, AC, BC)
1 HashMap with triple composite keys (ABC)
These would allow all the queries you need, I think. Having seven HashMaps may sound like a lot, but remember that much of the memory is shared - all the value objects are shared on the heap, as are many parts of the key objects. If you're adding and removing stuff over time, you'll want to wrap these seven maps in some other class which ensures that all put()/remove() operations keep the seven maps in sync. But if you're just loading them all once and then they're read-only, it's not too difficult.
Alternately, if you want to be able to expand to more complex queries (more than three keys), you may be better off letting SQL do the work for you. One possibility if you want to avoid expensive queries of your main DB but still be able to do complex search operations on the data, is to use something like HSQL to create a SQL-accessible in-memory database with all the info you need to be able to search on. Query the main DB once and put all the data you will need into the HSQL DB. Then later use SQL to query the HSQL for whatever you need. That may be a bit complex for what you've described so far, but could be worthwhile later if your search criteria become more complex than you've described so far.
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
Hi Jim,
I'm new to Java, but have 18 years in IT. This Hashtable is only my second Java "Production" project. I've gone from being at the top of the "knowledge" tree to the bottom. Rather humbling down here.
I showed your HSQL suggestion to the project lead and he got so excited he scared me.
We're implementing your suggestion.
Thank you for making me look good!
Lou
[ November 24, 2003: Message edited by: Lou P ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Excellent, good to hear it.
This conversation is actually extremely similar to another thread about a month ago, in which the HSQL possibility was pointed out by someone else here. I think it was a bartender, possibly Lasse - but I can't find that thread at the moment. Hopefully the other person will step up to claim credit where it's due.
Also, I should have said somethign about your display name, but I see it's already been covered here.
Welcome to the ranch then, and I hope you continue to find it useful.
[ November 24, 2003: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 3 Dimensional Hashtable
 
Similar Threads
Java Certfication
Arrays
Cloneable Object
Three Dimensional Array
3 Dimensional Hash Table