The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Accessing Hashkeys

Posted by Jason Tryon on June 01, 2001 at 3:01 PM

> Is it possible to use 1 value to get a hash key and to then store a different answer for the element

> EG.

> word1 = dog
> word2 = basketball

> we get the hashkey for word1 but instead of storing word1 as the element we store word2

> Any suggestions??


Your question is a little vague, but I think I'm on to what you're after. You have a hashtable and a key in the hashtable called word1 and you store a value of "dog" to it and then you want to over write that value with a value of "basketball". If I'm correct, read on. Otherwise disregard.

Keys in a hashtable are not necessarily unique. You could have to keys labled word1. However, doing so won't help you because hashtable.get(Object) returns the first one it finds so it would always return "dog" in this case. In my current project I am making extensive use of hashtables because the column content from the data could vary at the drop of a hat. I have methods to write hashtable entries where the code looks like this.

public void setKeyValue(Object hKey, Object hValue)
{
if ( ! "D".equals( getState() ) )
{
if ( isKey( hKey ) )
{
getCoreData().remove( hKey + "State" ) ;
getCoreData().put( hKey + "State", "E" ) ;
getCoreData().remove( hKey + "Changed" ) ;
getCoreData().put( hKey + "Changed", hValue ) ;
}
else
{
getCoreData().put( hKey, hValue ) ;
getCoreData().remove( hKey + "State" ) ;
getCoreData().put( hKey + "State", "A" ) ;
}

if ( ! "A".equals( getState() ) )
setState( "E" ) ;
}
}

isKey() checks to see if a key exists, its code is included below for reference. getCoreData() returns a reference to the coreData hashtable of the object.

public boolean isKey(Object hKey)
{
return getCoreData().containsKey(hKey) ;
}

hashtable.containsKey() returns true if the key exists and false if it does not. This simple vehicle allows us to quickly check for the existence of a proposed key.

This model includes three potential keys;

hKey (The actual key)
hKeyState (The conditional state of the key)
"X"=Unchanged, E=Edited, D=Deleted
hKeyChanged (The value of the key if it has been edited)

The logic in the class checks the state of the key on any attempt to read the key and if it has been deleted ignores the request, if it has been edited the request is automatically rerouted to the changed key. This allows us to roll back any changes without having to tie up database resources.

Regards,



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us