• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Lookup key in HashTable

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = list.next; // 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;
}
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeroen,

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
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Map.keySet() returns a Collection of all keys. You can then iterate over those.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't

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

working exactly the way you need it to work???
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

Layne
reply
    Bookmark Topic Watch Topic
  • New Topic