Hash tables with list or Vectors


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Hash tables with list or Vectors

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Posts
    4

    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

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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 ...

  3. #3
    Join Date
    Nov 2005
    Posts
    1

    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"

  4. #4
    Join Date
    Jun 2005
    Posts
    4

    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

  1. Replies: 0
    Last Post: 05-07-2002, 10:54 PM
  2. Replies: 0
    Last Post: 04-02-2002, 02:13 PM
  3. List of tables using ADODC
    By gs_london in forum VB Classic
    Replies: 1
    Last Post: 10-19-2001, 09:33 AM
  4. ListBot Going Out of Business
    By Larry Rebich in forum vb.announcements
    Replies: 1
    Last Post: 06-28-2001, 01:22 PM
  5. List all tables of a database
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center