-
Hash tables with list or Vectors
Hi All,
I would like to create a hash table that holds many values for one key, for this we might have to use stuff like lists along with hash tables(please let me know if there is any other better alternative for this!). It'll be great if someone has a very simple example of how to add elements and retrive many elements that belongs to a key. It'll be great if someone can give a couple of lines code illustrating how to do this.
Thanks
-
Do you have unique keys and the "value" you are associating with each key has many "data" components? If so, your "value" should be a class which holds those multiple components.
For example, let's say you are storing information about books in a hashtable ... and you create a hash key from the title of the book, and your "value" is a Book class. You now have lots of data (and methods for manipulating that data) associated with the single key ...
If you want the thing associated with the key to be a list, you can do that, too - you can associate any object with a key ...
-
Try this...................
Use HashMap and ArrayList instead of HashTable and Vector respectively for better perofmance, if you are sure that this information will not be modified concurrently.
Here is simple example describing the usage of HashMap and ArrayList.
Let us say you have three languages Java,C and Java Script.
The follwoing books are available for each language.
Java - Core Java, Complete Reference, Java Bible
C - Let us C, Programming with C
Java Script - Java Scritp:The definitive guide, Java script Bible
You have to store books of each language as a collection and you want retrieve all the books of particluar language by passing a key. Here the key is the language itself.
//Declare the variables
HashMap langMap = new HashMap();
ArrayList javaList = new ArrayList();
ArrayList cList = new ArrayList();
//Add the books to individual lists
ArrayList javaScriptList = new ArrayList();
javaList.add("Core Java");
javaList.add("Complete Reference");
javaList.add("Java Bible");
cList .add("Let us C");
cList .add("Programming with C");
javaScriptList .add("Java Scritp:The definitive guide");
javaScriptList .add("Java script Bible");
//Now add the lists to HashMap with language name as key
langMap.put("java",javaList);
langMap.put("c",cList);
langMap.put("javascript",javaScriptList);
//Now if want to retireve a specific list, then pass the key and retrieve the list
cList = langMap.gut("c");
//If u want to access inside the elements of the list
cList.get(0);//Returns "Let us C"
cList.get(1);//Returns "Programming with C"
-
thanks!!
Hi All,
Thanks for the quick replies!!!! I Am looking for something like one key and many values to store. In other words, say the data to be stored is something like this, Lanuage(Java, C, C++ etc..) and books(Java complete reference, Core Java, Learn Java in 24 hrs... all these should come under the category java(which is the key). Hope i'm clear! and when i'm printing it out, it should look like this..
Java Books
Java complete reference, Core Java, Learn Java in 24 hrs
etc...
So i need to access the key and all the elements under the same key.
Thanks for the help!
gn.
Similar Threads
-
Replies: 0
Last Post: 05-07-2002, 10:54 PM
-
Replies: 0
Last Post: 04-02-2002, 02:13 PM
-
By gs_london in forum VB Classic
Replies: 1
Last Post: 10-19-2001, 09:33 AM
-
By Larry Rebich in forum vb.announcements
Replies: 1
Last Post: 06-28-2001, 01:22 PM
-
By Helge B. derhelgeqhotmail.com in forum Database
Replies: 3
Last Post: 03-30-2001, 09:18 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks