dcsimg
How to get an ordered Hashtable
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   harshat_Taide
Posted On:   Monday, August 5, 2002 06:21 AM

Hi, I have a hashtable into which I put elements. I want to retrieve them in the order in which they are put. But when I equate this hashtable to an Enumeration Enumeration e = myHashtable.getKeys() and then I do e.nextElement() and get the keys, it gives the elements in a different order. Is there a way that I can order the elements of a Hashtable. Can I use a structure similar to Hashtable. Based on my program I constantly add and delete items in the hashtable. I am using the hashtable because I need to store my data in key value pair. Any help is appreciated. Thank You, Harsha    More>>

Hi,

I have a hashtable into which I put elements. I want to retrieve them in the order in which they are put. But when
I equate this hashtable to an Enumeration
Enumeration e = myHashtable.getKeys()

and then I do e.nextElement()



and get the keys, it gives the elements in a different order. Is there a way that I can order the elements of a
Hashtable. Can I use a structure similar to Hashtable. Based on my program I constantly add and delete items in the hashtable.


I am using the hashtable because I need to store my data in key value pair.





Any help is appreciated.



Thank You,

Harsha

   <<Less

Re: How to get an ordered Hashtable

Posted By:   Animesh_Srivastava  
Posted On:   Monday, August 5, 2002 08:44 PM

Hi Harsha,

I had stumbled upon a similar (infact exact) problem during one of my projects, where the requirements were such that we needed to retreive values from Hashtable in the same sequence as they were put in. Of course, java.util.Hashtable does not have this feature (as the Hashtable datastructure does not enforces this.) This is mentioned in java documentation too that neither the Iterator nor the Enumeration is guaranteed to retreive elements in the same order as they were put in. This is understandable because Hashtables are used more for their key-value lookup capability than anything else.


You can ofcourse write your own class (SequencedHashtable or OrderedHashtable etc) which enforces the ordering. Or you could use the one which I wrote during the project. It can be accessed on http://www.codeproject.com/useritems/OrderedHashtable.asp. This implementation enforces and guarantees ordering. You could consider using an ArrayList or something else also instead of Vector in that implementation.


Hope that helps.

Animesh.

Re: How to get an ordered Hashtable

Posted By:   Wolfram_Sorg  
Posted On:   Monday, August 5, 2002 07:19 AM

Nope...



The functionality that you are requesting ist not supported by the Hashtable class. The opportunity to work with Lists and Collections will not work here because you have key/value pairs and both should be in a specific order.



I don't see any other solution than implementing your own class. The easiest way would be something like the following:




import java.util.*;


public class
MyCollection
{
private ArrayList _keys; // the keys will be stored here
private ArrayList _vals; // corresponding values over here



public
MyCollection ()
{
// Simply create the global arrays

_keys = new ArrayList ();
_vals = new ArrayList ();
}



/**
* Return the number of entries
*/

public int
count ()
{
return _keys.size ();
}




/**
* Add a new key value pair.
*/

public void
addPair ( Object key, Object value )
throws NullPointerException
{
// Always check what you are dealing with...

if ( key == null || value == null ) {

throw new NullPointerException()
}


// We add the key object to the keys array and the value
// object to the values array

_keys.add ( key );
_vals.add ( value );
}



/**
* Get a specific key (note we are using the index position
* here, if count() would return 3 we would have the indices
* 0, 1 and 2).
*/

public Object
getKey ( int index )
throws IndexOutOfBounds
{
return _keys.get ( index );
}




/**
* The same with the value
*/

public Object
getValue ( int index )
throws IndexOutOfBoundsException
{
return _vals.get ( index );
}
}



The above example will return the entries in the same order as they were put into the collection. You could also add some kind of sorting. Removal and overwriting existing entries is also possible, simply use the corresponding methods from the Array class, as I did wit the getX methods. If you have further question... just ask ;)



WOS

Re: How to get an ordered Hashtable

Posted By:   Steven_Martin  
Posted On:   Monday, August 5, 2002 07:07 AM

A hashtable does not order its elements in that manner. You could have a seperate ordered list of your keys (TreeSet) and still have your hashtable. Also, if the ordering is used rarely enough, created a List with the keys and Collections.sort will sort it for you.
About | Sitemap | Contact