About Hashtable...

M

M

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.
 
S

Steve Horsley

M said:
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

M

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....
 
C

Chris Smith

M said:
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

M

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?
 
M

Mykola Rabchevskiy

M said:
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 ...
}
 
V

Virgil Green

M said:
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
 
R

Roedy Green

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.
 
M

M

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

John C. Bollinger

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.
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
(e-mail address removed)
 
V

Virgil Green

M said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top