About Hashtable...

Discussion in 'Java' started by M, Jul 11, 2004.

  1. M

    M Guest

    Hi all,

    I'm using a hashtable to store some key/values.
    However, I have trouble in using the keySet() method to
    get a list of keys in the hashtable.

    The keySet() method simplys return a Set Object.
    How can I declare and use it?

    Thanks.
     
    M, Jul 11, 2004
    #1
    1. Advertising

  2. M wrote:
    > Hi all,
    >
    > I'm using a hashtable to store some key/values.
    > However, I have trouble in using the keySet() method to
    > get a list of keys in the hashtable.
    >
    > The keySet() method simplys return a Set Object.
    > How can I declare and use it?
    >
    > Thanks.
    >
    >


    If you really want a List of the keys you can do something like:

    List keyList = new ArrayList(myHashtable.keySet());

    but I guess you most likely just want to iterate over the key
    values with something like:

    Iterator it = myHashtable.keySet().iterator();
    while(it.hasNext()) {
    System.out.println("Key:" + it.next());
    }


    Steve
     
    Steve Horsley, Jul 11, 2004
    #2
    1. Advertising

  3. M

    M Guest

    Thanks. I also wondering if there's something, beside hashtable, in Java
    which can be processed like a database table. So that I can have key
    read, sequential scan, etc....

    "Steve Horsley" <> wrote in message
    news:ccs1bu$7ov$2surf.net...
    > M wrote:
    > > Hi all,
    > >
    > > I'm using a hashtable to store some key/values.
    > > However, I have trouble in using the keySet() method to
    > > get a list of keys in the hashtable.
    > >
    > > The keySet() method simplys return a Set Object.
    > > How can I declare and use it?
    > >
    > > Thanks.
    > >
    > >

    >
    > If you really want a List of the keys you can do something like:
    >
    > List keyList = new ArrayList(myHashtable.keySet());
    >
    > but I guess you most likely just want to iterate over the key
    > values with something like:
    >
    > Iterator it = myHashtable.keySet().iterator();
    > while(it.hasNext()) {
    > System.out.println("Key:" + it.next());
    > }
    >
    >
    > Steve
     
    M, Jul 12, 2004
    #3
  4. M

    Roedy Green Guest

    On Mon, 12 Jul 2004 10:37:43 +0800, "M" <> wrote
    or quoted :

    >Thanks. I also wondering if there's something, beside hashtable, in Java
    >which can be processed like a database table. So that I can have key
    >read, sequential scan, etc....


    see http://mindprod.com/jgloss/collection.html


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 12, 2004
    #4
  5. M

    Chris Smith Guest

    M wrote:
    > Thanks. I also wondering if there's something, beside hashtable, in Java
    > which can be processed like a database table. So that I can have key
    > read, sequential scan, etc....


    Although there plenty of possible uses of the Collections API that could
    meet some of these needs, if what you want is a database, then look at
    hsqldb, which is a simple SQL engine that can be embedded into Java
    code.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 12, 2004
    #5
  6. M

    M Guest

    Thanks. But my assignment simply does not allow me to use
    any SQL database... so, what I targeting is to use
    hashtable but have problem to sort it by the key.

    Any hint?

    "Chris Smith" <> wrote in message
    news:...
    > M wrote:
    > > Thanks. I also wondering if there's something, beside hashtable, in

    Java
    > > which can be processed like a database table. So that I can have key
    > > read, sequential scan, etc....

    >
    > Although there plenty of possible uses of the Collections API that could
    > meet some of these needs, if what you want is a database, then look at
    > hsqldb, which is a simple SQL engine that can be embedded into Java
    > code.
    >
    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation
     
    M, Jul 12, 2004
    #6
  7. M wrote:

    > Thanks. But my assignment simply does not allow me to use
    > any SQL database... so, what I targeting is to use
    > hashtable but have problem to sort it by the key.
    >
    > Any hint?
    >


    Vector keyList = new Vector( someHashtable.keySet() );
    Collections.sort( keyList );
    Object key;
    for( Iterator keyIterator = keyList.iterator(); i.hasNext(); ){
    Object value = someHashtable.get( key = keyIterator.next() );
    // ... use key and value ...
    }
     
    Mykola Rabchevskiy, Jul 12, 2004
    #7
  8. M

    Virgil Green Guest

    "M" <> wrote in message
    news:ccudp6$2hsa$...
    > Thanks. But my assignment simply does not allow me to use
    > any SQL database... so, what I targeting is to use
    > hashtable but have problem to sort it by the key.
    >
    > Any hint?


    Why are you using a hashtable? Is there a particular need? You want a sorted
    list so why not use something like TreeSet?

    - Virgil
     
    Virgil Green, Jul 12, 2004
    #8
  9. M

    Roedy Green Guest

    On Mon, 12 Jul 2004 10:37:43 +0800, "M" <> wrote
    or quoted :

    >Thanks. I also wondering if there's something, beside hashtable, in Java
    >which can be processed like a database table. So that I can have key
    >read, sequential scan, etc....


    You can export the keys/values and sort them as an array.
    You can maintain several collections on your objects, a HashMap for
    fast lookup and various TreeSets/TreeMaps to keep them in various
    orders.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 12, 2004
    #9
  10. M

    M Guest

    Cos TreeSet gives me no option to read by key, as specified in API spec.
    Any better idea other than Hashtable?

    "Virgil Green" <> wrote in message
    news:7_zIc.14776$...
    >
    > "M" <> wrote in message
    > news:ccudp6$2hsa$...
    > > Thanks. But my assignment simply does not allow me to use
    > > any SQL database... so, what I targeting is to use
    > > hashtable but have problem to sort it by the key.
    > >
    > > Any hint?

    >
    > Why are you using a hashtable? Is there a particular need? You want a

    sorted
    > list so why not use something like TreeSet?
    >
    > - Virgil
    >
    >
     
    M, Jul 13, 2004
    #10
  11. Please don't top-post. It makes it rather difficult to follow the
    conversation. I have flip flopped your response and the previous
    poster's comments to make it read sensibly.

    M wrote:
    > "Virgil Green" <> wrote in message
    > news:7_zIc.14776$...
    >
    >>"M" <> wrote in message
    >>news:ccudp6$2hsa$...
    >>
    >>>Thanks. But my assignment simply does not allow me to use
    >>>any SQL database... so, what I targeting is to use
    >>>hashtable but have problem to sort it by the key.
    >>>
    >>>Any hint?

    >>
    >>Why are you using a hashtable? Is there a particular need? You want a

    >
    > sorted
    >
    >>list so why not use something like TreeSet?


    > Cos TreeSet gives me no option to read by key, as specified in API spec.
    > Any better idea other than Hashtable?


    Virgil may have meant to suggest a TreeMap, which might indeed do what
    you want. Really, though, you have not been very detailed about what
    "database-like" functionality you need. If TreeMap doesn't do it for
    you then you probably will have to explain clearly what you do need.
    The exercise of figuring that out will likely be useful in completing
    your assignment anyway.


    John Bollinger
     
    John C. Bollinger, Jul 13, 2004
    #11
  12. M

    Virgil Green Guest

    "M" <> wrote in message
    news:cd0q4t$ouj$...
    > "Virgil Green" <> wrote in message
    > news:7_zIc.14776$...
    > >
    > > "M" <> wrote in message
    > > news:ccudp6$2hsa$...
    > > > Thanks. But my assignment simply does not allow me to use
    > > > any SQL database... so, what I targeting is to use
    > > > hashtable but have problem to sort it by the key.
    > > >
    > > > Any hint?

    > >
    > > Why are you using a hashtable? Is there a particular need? You want a

    > sorted
    > > list so why not use something like TreeSet?

    >
    > Cos TreeSet gives me no option to read by key, as specified in API spec.
    > Any better idea other than Hashtable?


    As pointed out by John, I should have recommended TreeMap rather than
    TreeSet.

    - Virgil
     
    Virgil Green, Jul 13, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guillermo

    Problem with hashTable

    Guillermo, Mar 4, 2004, in forum: Perl
    Replies:
    1
    Views:
    618
    Gunnar Hjalmarsson
    Mar 4, 2004
  2. Jonathan Wolfson

    vbc compilation fails when using Hashtable

    Jonathan Wolfson, Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    559
    Tu-Thach
    Jun 27, 2003
  3. John E

    Get Hashtable Object Directly

    John E, Oct 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,334
    Nicholas Paldino [.NET/C# MVP]
    Oct 8, 2003
  4. diya

    Type Hashtable not defined

    diya, Oct 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,794
    Nicole Calinoiu
    Oct 31, 2003
  5. D. Shane Fowlkes

    ArrayList versus HashTable

    D. Shane Fowlkes, Feb 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    17,794
    Kevin Spencer
    Feb 12, 2004
Loading...

Share This Page