Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Lookup key in HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Lookup key in HashTable" Watch "Lookup key in HashTable" New topic

Lookup key in HashTable

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

I'm lost. I have created a HashTable in my constructor of a web service that I am working on and is being populated with Transaction ID's:

public class Transaction {
static int TransactionId = 0;
static Hashtable Transactions = new Hashtable();
int Id;

// Constructor created to increment the TransactionID's and put
// into Transactions Hashtable.
public Transaction(){
this.Id = TransactionId++;
Transactions.put(new Integer(TransactionId), this);

// Method created to grab the TransactionId for the BeginTransactionResponse class.
public int GetId(){
return Id;

// Method created to LookUpTransaction from the Transactions hashtable.
public Object get(Transactions
int idx = hash(TransactionId);

return Transactions.get(new Integer(Transaction.TransactionId));

As as you can tell from the last method above, I was trying to create a LoopUpTransaction method whereby I can pass in a Transaction ID to see if it exists in the Hash Table. I have googled for this solution but all I found that made sense was this code but I keep getting squigglies under the BOLDED HASH statement below, any help would be appreciated:

public Object get(Object key) {
// Retrieve the value associated with the specified key
// in the table, if there is any. If not, the value
// null will be returned.
int bucket = hash(key); // At what location should the key be?
ListNode list = table[bucket]; // For traversing the list.
while (list != null) {
// Check if the specified key is in the node that
// list points to. If so, return the associated value.
if (list.key.equals(key))
return list.value;
list =; // Move on to next node in the list.
// If we get to this point, then we have looked at every
// node in the list without finding the key. Return
// the value null to indicate that the key is not in the table.
return null;
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
To check if a key exists in a Map (which a Hashtable is), just use the hasKey(Object) function on the Map.

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387


I was wrong. I need to be able to return the VALUE of the key not see if the key exists in the hashtable. My apologies. I'm giving this another go and see if I can get this.

Thanks for your time and reply.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Map.keySet() returns a Collection of all keys. You can then iterate over those.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

Transactions.get(new Integer(Transaction.TransactionId));

working exactly the way you need it to work???

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You should familiarize yourself with the Java API documentation for the version of Java you are using. It provides a great reference to find methods of specific classes that will accomplish the task at hand.

However, you also need to know what classes and methods are available. One place to start is the Java Tutorial. In particular, you may find that the Collections trail is a good resource as an overview of what is available from the Collections API (which includes HashMap).

If you look through this documentation, you will quickly find that a simple call to HashMap.get() will do what you need just as Ilja was trying to point out, unless I completly misunderstood your question, of course.

I hope this helps.

Keep Coding!


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Lookup key in HashTable
It's not a secret anymore!