Results 1 to 7 of 7

Thread: hashtable

  1. #1
    DaoK
    Guest

    hashtable

    Hello!

    I need a answer quick
    How can I check if a key is already taken in a hashtable ?
    I wanted to loop in all object in the hastable but I can not see how to loop because in hashtable we do not have any index...Any one can help me ?

    This is an example of what I want to do:
    -----------------------------------------------------
    I have added 3 elements in my hastable:

    myHashTable.put ("101", Object1);
    myHashTable.put ("1241", Object2;
    myHashTable.put ("19, Object3;

    Now i need a piece of code to loop in the hashtable to be able to check the key of each...how can i do that ???

  2. #2
    DaoK
    Guest
    I solved my problem with :
    Code:
     if(ymhasTable.containsKey ("101")==true)
        System.out.println("Key exist");

  3. #3
    DaoK
    Guest
    But I still need to find a way to loop in all element of a hashtable so please if you know how to loop through a hashtable please give me some line of code.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Hashtable has a keys() method which returns an Enumeration of the keys. You can loop through that and do your comparisions.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    DaoK
    Guest
    Thx you I will check this out tommorow, but if you have time I will appreaciate a little example

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    Hashtable ht = new Hashtable();
    //Load Hashtable at some point.
    
    Enumeration e = ht.keys();
    
    while(e.hasMoreElements())
    {
        System.out.println(e.nextElement().toString());
    }
    That's how the Enumeration looping will look. Just replace the code inside the while loop with your code.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    DaoK
    Guest
    Big thx I have tryed so many thing, but your way is good Thx you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width