This page was last modified 23:02, 3 July 2008.
A Simple Ordered Hashtable
From Forum Nokia Wiki
This article illustrates how to implement an ordered hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.
As with typical Hashtables, to successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
There are many instances where you would like to use an ordered Hashtable, for example, to keep your user interface elements ordered, or to keep ordered items from a database or backend while keeping rapid access via Hashtable keys, or to store and access any value you want to access using a key.
This ordered Hashtable is called simple because internally it uses the Legacy collection classes, a Vector to maintain the element's order and a Hashtable to provide hashing capabilities. Because Hashtable and Vector grow differently, the implementation of
A Hashtable implementation
SimpleOrderedHashtable is not the most efficient one, but may be good enough for your needs.
import java.util.Vector; import java.util.Enumeration; import java.util.Hashtable; /** * Implements an Ordered Hashtable, with elements in * chronological order (i.e. insertion order) */ public class SimpleOrderedHashtable { private Vector orderedKeys; private Hashtable hashTable; /** * Constructor, creates an SimpleOrderedHashtable. */ public SimpleOrderedHashtable() { orderedKeys = new Vector(); hashTable = new Hashtable(); } /** * Constructor, creates an SimpleOrderedHashtable. * @param initialCapacity is the initial size for the container. */ public SimpleOrderedHashtable(int initialCapacity) { orderedKeys = new Vector(initialCapacity); hashTable = new Hashtable(initialCapacity); } /** * Maps the specified key to the specified value in this SimpleOrderedHashtable. * The value can be retrieved by calling the get method with a key that is * equal to the original key. * @param key is the hashtable key. * @param value is the value. * @return the previous value of the specified key in this * SimpleOrderedHashtable, or null if it did not have one. */ synchronized public Object put(Object key, Object value) { int i = orderedKeys.indexOf(key); if (i == -1) { // Add new name/value pair. orderedKeys.addElement(key); // insert (append) to the end of the list } else { // Replace name/value pair. orderedKeys.setElementAt(key, i); } return hashTable.put(key, value); } /** * Returns the value to which the specified key is mapped in this * hashtable. * @param key is a key in the SimpleOrderedHashtable. * @return the value to which the key is mapped in this hashtable; null if * the key is not mapped to any value in this hashtable. */ synchronized public Object get(Object key) { return hashTable.get(key); } /** * Returns an enumeration of the keys in this SimpleOrderedHashtable. * @return an enumeration of the keys in this SimpleOrderedHashtable. */ synchronized public Enumeration keys() { return orderedKeys.elements(); } /** * Returns an enumeration of the elements in this SimpleOrderedHashtable. * @return an enumeration of the elements in this SimpleOrderedHashtable. */ synchronized public Enumeration elements() { int s = hashTable.size(); Vector elements = new Vector(s); for (int i=0; i<s; i++) { elements.addElement(elementAt(i)); } return elements.elements(); } /** * Returns the component at the specified index. * @param index is an index into this SimpleOrderedHashtable. * @return the <code>Object</code> component at the specified index. * @throws ArrayIndexOutOfBoundsException if index is out of bounds. */ synchronized public Object elementAt(int index) throws ArrayIndexOutOfBoundsException { Object key = orderedKeys.elementAt(index); return hashTable.get(key); } /** * Returns the key at the specified index. * @param index is an index into this SimpleOrderedHashtable. * @return the <code>Object</code> key at the specified index. * @throws ArrayIndexOutOfBoundsException if index is out of bounds. */ synchronized public Object keyAt(int index) throws ArrayIndexOutOfBoundsException { return orderedKeys.elementAt(index); } /** * Returns the index of the specified <code>Object</code>. * @param key is a key in the SimpleOrderedHashtable. * @return the index of the specified <code>Object</code>. */ synchronized public int getIndex(Object key) { return orderedKeys.indexOf(key); } /** * Removes the key (and its corresponding value) from this hashtable. This * method does nothing if the key is not in the hashtable. * @param key is the key that needs to be removed. */ synchronized public void remove(Object key) { orderedKeys.removeElement(key); hashTable.remove(key); } /** * Removes an element at the specified index. * @param i is the index of the element to remove. */ synchronized public void removeElementAt(int i) { Object key = orderedKeys.elementAt(i); orderedKeys.removeElementAt(i); hashTable.remove(key); } /** * Clears this SimpleOrderedHashtable so that it contains no keys. */ synchronized public void clear() { orderedKeys.removeAllElements(); hashTable.clear(); } /** * Returns the number of components in this SimpleOrderedHashtable. * @return the number of components in this vector. */ synchronized public int size() { return orderedKeys.size(); } /** * Recomputes the SimpleOrderedHashtable capacity. * @param capacity is the capacity to ensure. */ synchronized public void ensureCapacity(int capacity) { orderedKeys.ensureCapacity(capacity); } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| some tutorials required for designing simple bluetooth multiplayer games | tango85 | Mobile Java General | 5 | 2007-04-13 12:59 |
| Nokia 6610 + Windows Hyperterminal | santanubiswas | PC Suite API and PC Connectivity SDK | 7 | 2006-03-29 00:43 |
| Listbox in a simple GUI application | alav | Symbian User Interface | 12 | 2007-12-05 20:17 |
| How to start? | ahsp83 | General Symbian C++ | 1 | 2005-11-22 19:13 |
| Symbian Software... | Nokia_Archive | General Symbian C++ | 1 | 2002-05-29 13:19 |
