Usually hashtabel will print the elements in reverse
order. here is a class which you can use to sort them
in the order in which they were added.

as an additional here is the code for Map also.



=====


____________________________________________________________
Do You Yahoo!?
Send a newsletter, share photos & files, conduct polls, organize chat events. Visit 
http://in.groups.yahoo.com
package com.ibm.ivj.db.base;

/**
 * This class was generated by a SmartGuide.
 *
 */
import java.util.*;
public class OrderedHashtable extends Dictionary implements java.io.Serializable {
private Vector sequenceableKeys;
private Vector sequenceableValues;
private Hashtable hashtable;
        // VersionUID for Version 1.0
        static final long serialVersionUID = 6383502102138858878L;
        private final static String copyright = "Licensed Materials -- Property of 
IBM\n(c) Copyright International Business Machines Corporation, 1998, 1999";
/**
 * This method was created by a SmartGuide.
 */
public OrderedHashtable ( ) {
this(101);
}
/**
 * This method was created by a SmartGuide.
 */
public OrderedHashtable (int initialCapacity) {
sequenceableKeys = new Vector();
sequenceableValues = new Vector();
hashtable = new Hashtable(initialCapacity);
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized boolean contains(Object value) {
        return sequenceableValues.contains(value);
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized boolean containsKey(Object key) {
        return sequenceableKeys.contains(key);
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
private synchronized void decrementIndexesBetween (int start, int stop) {
        Enumeration enum =      hashtable.keys();
        Object key;
        int temp;
        while (enum.hasMoreElements()) {
                key = enum.nextElement();
                if (start <= (temp = ((Integer)(hashtable.get(key))).intValue()) &&
                                (temp <= stop)) {
                        hashtable.put(key,new Integer(--temp));
                }
        }
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Enumeration elements() {
        return sequenceableValues.elements();
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object get (Object key) {
Object anObject,anIndex;
        try {
                anIndex = hashtable.get(key);
                if (anIndex == null) {
                        return null;
                }
                else anObject = 
sequenceableValues.elementAt(((Integer)anIndex).intValue() - 1);
        }
        catch (ArrayIndexOutOfBoundsException ex) {
                return null;
        }
        return anObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object getAtIndex (int index) {
        Object anObject;
        try {
                anObject = sequenceableValues.elementAt(index - 1);
        }
        catch (ArrayIndexOutOfBoundsException ex) {
                return null;
        }
        return anObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
private synchronized void incrementIndexesBetween (int start, int stop) {
        Enumeration enum =      hashtable.keys();
        Object key;
        int temp;
        while (enum.hasMoreElements()) {
                key = enum.nextElement();
                if (start <= (temp = ((Integer)(hashtable.get(key))).intValue()) &&
                                (temp <= stop)) {
                        hashtable.put(key,new Integer(++temp));
                }
        }
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized int indexOfKey (Object key) {
Object anObject,anIndex;
        try {
                anIndex = hashtable.get(key);
                if (anIndex == null) {
                        return 0;
                }
                else return ((Integer)anIndex).intValue();
        }
        catch (ArrayIndexOutOfBoundsException ex) {
                return 0;
        }
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public boolean isEmpty() {
        return hashtable.isEmpty();
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Enumeration keys() {
        return sequenceableKeys.elements();
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
private Integer newIndex () {

        return new Integer(sequenceableValues.size() + 1);
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object put (Object key, Object value) {
        Integer index;
        Object returnObject;
        if ((index = (Integer)hashtable.get(key)) == null) {
                index = newIndex();
                hashtable.put(key,index);
                sequenceableKeys.addElement(key);
                sequenceableValues.addElement(value);
                returnObject = null;
        }
        else {
                returnObject = sequenceableValues.elementAt(index.intValue() - 1);

        }
        return returnObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object putAfterIndex (Object key, Object value,int index) {
        Integer oldIndex,newIndex;
        Object returnObject,indexKey;
        if ( (sequenceableValues.size() + 1) < index) {
                return null;
        }
        if ((oldIndex = (Integer)hashtable.get(key)) == null) {
                incrementIndexesBetween(index + 1,sequenceableValues.size() + 1);
                hashtable.put(key,new Integer(index + 1));
                sequenceableKeys.insertElementAt(key,index);
                sequenceableValues.insertElementAt(value,index);
                returnObject = null;
        }
        else {
                indexKey = sequenceableKeys.elementAt(index - 1);
                removeElementAt(oldIndex.intValue());
                newIndex = (Integer)hashtable.get(indexKey);
                putAfterIndex (key,value,newIndex.intValue());
                returnObject = value;
        }
        return returnObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object putBeforeIndex (Object key, Object value,int index) {
        Integer oldIndex,newIndex;
        Object returnObject,indexKey;
        if ( sequenceableValues.size() < index) {
                return null;
        }
        if ((oldIndex = (Integer)hashtable.get(key)) == null) {
                incrementIndexesBetween(index,sequenceableValues.size());
                hashtable.put(key,new Integer(index));
                sequenceableKeys.insertElementAt(key,index - 1);
                sequenceableValues.insertElementAt(value,index - 1);
                returnObject = null;
        }
        else {
                indexKey = sequenceableKeys.elementAt(index - 1);
                removeElementAt(oldIndex.intValue());
                newIndex = (Integer)hashtable.get(indexKey);
                putAfterIndex (key,value,newIndex.intValue());
                returnObject = value;
        }
        return returnObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object remove (Object key) {
        Integer index;
        Object returnObject;
        if ((index = (Integer)hashtable.get(key)) == null) {
                returnObject = null;
        }
        else {
                decrementIndexesBetween((index.intValue() + 
1),sequenceableValues.size());
                returnObject = hashtable.remove(key);
                sequenceableKeys.removeElementAt(index.intValue() - 1);
                sequenceableValues.removeElementAt(index.intValue() - 1);
        }
        return returnObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public synchronized Object removeElementAt (int index) {

        Object returnObject,key;
        if ( sequenceableValues.size() < index) {
                return null;
        }
        key = sequenceableKeys.elementAt(index - 1);
        if (hashtable.get(key) == null) {
                returnObject = null;
        }
        else {
                decrementIndexesBetween((index + 1),sequenceableValues.size());
                returnObject = hashtable.remove(key);
                sequenceableKeys.removeElementAt(index - 1);
                sequenceableValues.removeElementAt(index - 1);
        }
        return returnObject;
}
/**
 * This method was created by a SmartGuide.
 * @return java.lang.Object
 * @param key java.lang.Object
 */
public int size() {
        return hashtable.size();
}
        /**
         *
         * @return  a string representation of this ordered hashtable.
         */
public synchronized String toString() {
        int max = size() - 1;
        StringBuffer buf = new StringBuffer();
        Enumeration k = keys();
        Enumeration e = elements();
        buf.append("{");

        for (int i = 0; i <= max; i++) {
            String s1 = k.nextElement().toString();
            String s2 = e.nextElement().toString();
            buf.append("(" + (new Integer(i + 1)).toString() + ")" + s1 + "=" + s2);
            if (i < max) {
                buf.append(", \n");
            }
        }
        buf.append("}");
        return buf.toString();
        }
}
package com.eds.fi.ebca.ngbs.util;

import java.util.*;

/**
 * Insert the type's description here.
 * Creation date: (04/04/2001 17:00:42)
 * @author: Teppei Nakano
 */
public class OrderedMap extends AbstractMap implements SortedMap, 
java.io.Serializable, Cloneable
{
   //   private final TreeMap map = new TreeMap();
   //   private final HashMap keys = new HashMap();
   private final OrderedSet keys = new OrderedSet();
   private final HashMap values = new HashMap();
   private transient Set entrySetView;
   private int count = 0;

   private class EntrySetView extends OrderedSetView
   {
      public Iterator iterator()
      {
         return new OrderedIterator( OrderedIterator.ENTRY );
      }
   }


   private transient Set keySetView;
   private transient Set valueSetView;

   private class KeySetView extends OrderedSetView
   {
      public Iterator iterator()
      {
         return new OrderedIterator( OrderedIterator.KEY );
      }
   }


   private class OrderedIterator implements Iterator, java.io.Serializable
   {
      private static final int ENTRY = 0;
      private static final int KEY   = 1;
      private static final int VALUE = 2;
      private int type               = 0;
      private Iterator keys          = OrderedMap.this.keys.iterator();
      private Object lastKey         = null;

      public OrderedIterator( int type )
      {
         this.type = type;
      }

      public Object next()
      {
         this.lastKey = keys.next();
         Object value = OrderedMap.this.values.get( lastKey );
         switch ( type )
         {
            case ENTRY:
               return new Pair( lastKey, value );
            case KEY:
               return lastKey;
            case VALUE:
               return value;
            default:
               throw new IllegalStateException( "OrderedMap.OrderedIterator: 
IllegalType.:" + type );
         }
      }

      public boolean hasNext()
      {
         return keys.hasNext();
      }

      public void remove()
      {
         if ( lastKey == null )
         {
            throw new IllegalStateException( "next must be called first." );
         }
         keys.remove();
         OrderedMap.this.values.remove( lastKey );
      }
   }


   private abstract class OrderedSetView extends AbstractSet implements SortedSet, 
java.io.Serializable
   {
      public int size()
      {
         return keys.size();
      }

      public Object first()
      {
         throw new UnsupportedOperationException();
      }

      public Object last()
      {
         throw new UnsupportedOperationException();
      }

      public SortedSet headSet( Object obj )
      {
         throw new UnsupportedOperationException();
      }

      public SortedSet tailSet( Object obj )
      {
         throw new UnsupportedOperationException();
      }

      public SortedSet subSet( Object obj1, Object obj2 )
      {
         throw new UnsupportedOperationException();
      }

      public Comparator comparator()
      {
         throw new UnsupportedOperationException();
      }
   }


   private class ValueSetView extends OrderedSetView
   {
      public Iterator iterator()
      {
         return new OrderedIterator( OrderedIterator.VALUE );
      }
   }


   /**
    * OrderSortedMap constructor.
    */
   public OrderedMap()
   {
      super();
   }

   /**
    * OrderSortedMap constructor comment.
    */
   public OrderedMap( Map map )
   {
      super();
      this.putAll( map );
   }

   /**
    * Returns the comparator associated with this sorted map, or
    * <tt>null</tt> if it uses its keys' natural ordering.
    *
    * @return the comparator associated with this sorted map, or
    *          <tt>null</tt> if it uses its keys' natural ordering.
    */
   public java.util.Comparator comparator()
   {
      return null;
   }

   /**
    * Returns a set view of the mappings contained in this map.  Each element
    * in the returned set is a <tt>Map.Entry</tt>.  The set is backed by the
    * map, so changes to the map are reflected in the set, and vice-versa.
    * If the map is modified while an iteration over the set is in progress,
    * the results of the iteration are undefined.  The set supports element
    * removal, which removes the corresponding mapping from the map, via the
    * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
    * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not support
    * the <tt>add</tt> or <tt>addAll</tt> operations.
    *
    * @return a set view of the mappings contained in this map.
    */
   public java.util.Set entrySet()
   {
      if ( entrySetView == null )
      {
         this.entrySetView = new EntrySetView();
      }
      return entrySetView;
   }

   /**
    * Returns the first (lowest) key currently in this sorted map.
    *
    * @return the first (lowest) key currently in this sorted map.
    * @throws    NoSuchElementException if this map is empty.
    */
   public Object firstKey()
   {
      return keys.first();
   }

   /**
    * Returns a view of the portion of this sorted map whose keys are
    * strictly less than toKey.  The returned sorted map is backed by this
    * sorted map, so changes in the returned sorted map are reflected in this
    * sorted map, and vice-versa.  The returned map supports all optional map
    * operations that this sorted map supports.<p>
    *
    * The map returned by this method will throw an IllegalArgumentException
    * if the user attempts to insert a key outside the specified range.<p>
    *
    * Note: this method always returns a view that does not contain its
    * (high) endpoint.  If you need a view that does contain this endpoint,
    * and the key type allows for calculation of the successor a given
    * key, merely request a headMap bounded by successor(highEndpoint).
    * For example, suppose that suppose that <tt>m</tt> is a map whose keys
    * are strings.  The following idiom obtains a view containing all of the
    * key-value mappings in <tt>m</tt> whose keys are less than or equal to
    * <tt>high</tt>:
    *
    *       <pre>    Map head = m.headMap(high+"\0");</pre>
    *
    * @param toKey high endpoint (exclusive) of the subMap.
    * @return a view of the specified initial range of this sorted map.
    *
    * @throws ClassCastException if <tt>toKey</tt> cannot be compared with
    *             the keys currently in the sorted map.  (Implementations may,
    *             but are not required to, throw this exception under these
    *             circumstances.)
    * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
    *             this sorted map does not tolerate <tt>null</tt> keys.
    */
   public java.util.SortedMap headMap( Object toKey )
   {
      throw new UnsupportedOperationException();
   }

   public java.util.Set keySet()
   {
      if ( keySetView == null )
      {
         this.keySetView = new KeySetView();
      }
      return keySetView;
   }

   /**
    * Returns the last (highest) key currently in this sorted map.
    *
    * @return the last (highest) key currently in this sorted map.
    * @throws     NoSuchElementException if this map is empty.
    */
   public Object lastKey()
   {
      return keys.last();
   }

   public Object put( Object key, Object value )
   {
      keys.add( key );
      return values.put( key, value );
   }

   /**
    * Returns a view of the portion of this sorted map whose keys range from
    * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.  (If
    * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned sorted map
    * is empty.)  The returned sorted map is backed by this sorted map, so
    * changes in the returned sorted map are reflected in this sorted map,
    * and vice-versa.  The returned Map supports all optional map operations
    * that this sorted map supports.<p>
    *
    * The map returned by this method will throw an
    * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
    * outside the specified range.<p>
    *
    * Note: this method always returns a <i>half-open range</i> (which
    * includes its low endpoint but not its high endpoint).  If you need a
    * <i>closed range</i> (which includes both endpoints), and the key type
    * allows for calculation of the successor a given key, merely request the
    * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
    * For example, suppose that <tt>m</tt> is a map whose keys are strings.
    * The following idiom obtains a view containing all of the key-value
    * mappings in <tt>m</tt> whose keys are between <tt>low</tt> and
    * <tt>high</tt>, inclusive:
    *
    *       <pre>    Map sub = m.subMap(low, high+"\0");</pre>
    *
    * A similarly technique can be used to generate an <i>open range</i>
    * (which contains neither endpoint).  The following idiom obtains a
    * view containing  all of the key-value mappings in <tt>m</tt> whose keys
    * are between <tt>low</tt> and <tt>high</tt>, exclusive:
    *
    *       <pre>    Map sub = m.subMap(low+"\0", high);</pre>
    *
    * @param fromKey low endpoint (inclusive) of the subMap.
    * @param toKey high endpoint (exclusive) of the subMap.
    * @return a view of the specified range within this sorted map.
    *
    * @throws ClassCastException if <tt>fromKey</tt> or <tt>toKey</tt> cannot
    *             be compared with the keys currently in the sorted map.
    *             (Implementations may, but are not required to, throw this
    *             exception under these circumstances.)
    * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
    *             <tt>null</tt> and this sorted map does not tolerate
    *             <tt>null</tt> keys.
    * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
    *         <tt>toKey</tt>.
    */
   public java.util.SortedMap subMap( Object fromKey, Object toKey )
   {
      throw new UnsupportedOperationException();
   }

   /**
    * Returns a view of the portion of this sorted map whose keys are greater
    * than or equal to <tt>fromKey</tt>.  The returned sorted map is backed
    * by this sorted map, so changes in the returned sorted map are reflected
    * in this sorted map, and vice-versa.  The returned map supports all
    * optional map operations that this sorted map supports.<p>
    *
    * The map returned by this method will throw an
    * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
    * outside the specified range.<p>
    *
    * Note: this method always returns a view that contains its (low)
    * endpoint.  If you need a view that does not contain this endpoint, and
    * the element type allows for calculation of the successor a given value,
    * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
    * For example, suppose that suppose that <tt>m</tt> is a map whose keys
    * are strings.  The following idiom obtains a view containing all of the
    * key-value mappings in <tt>m</tt> whose keys are strictly greater than
    * <tt>low</tt>:
    *
    *       <pre>    Map tail = m.tailMap(low+"\0");</pre>
    *
    * @param fromKey low endpoint (inclusive) of the tailMap.
    * @return a view of the specified final range of this sorted map.
    *
    * @throws ClassCastException if <tt>fromKey</tt> cannot be compared with
    *             the keys currently in the sorted map.  (Implementations may,
    *             but are not required to, throw this exception under these
    *             circumstances.)
    *
    * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
    *         this sorted map does not tolerate <tt>null</tt> keys.
    */
   public java.util.SortedMap tailMap( Object fromKey )
   {
      throw new UnsupportedOperationException();
   }

   public java.util.Collection values()
   {
      if ( valueSetView == null )
      {
         this.valueSetView = new ValueSetView();
      }
      return valueSetView;
   }
}

Reply via email to