Hash tables with list or Vectors
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.
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 ...
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
cList .add("Let us C");
cList .add("Programming with C");
//Now add the lists to HashMap with language name as key
//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 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 complete reference, Core Java, Learn Java in 24 hrs
So i need to access the key and all the elements under the same key.
Thanks for the help!
Last Post: 05-07-2002, 10:54 PM
Last Post: 04-02-2002, 02:13 PM
By gs_london in forum VB Classic
Last Post: 10-19-2001, 09:33 AM
By Larry Rebich in forum vb.announcements
Last Post: 06-28-2001, 01:22 PM
By Helge B. derhelgeqhotmail.com in forum Database
Last Post: 03-30-2001, 09:18 AM
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