Class OrderedHashtable<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>

    public class OrderedHashtable<K,​V>
    extends java.util.Hashtable<K,​V>
    A Hashtable implementation maintaining the values in the order they were inserted. Note that the order is only maintained on the put() and get() functions and the ordered values can only be accessed through the values() function. All other methods that operate on other Maps or on keys do not provide any guarantee for the ordering of the values if not otherwise mentioned. Further note that in order to maintain the order of the values additional space and time overhead is introduced to the standard Hashtable implementation. This object should be threadsafe, every value changing method is synchronized.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      OrderedHashtable()
      Constructs a new, empty ordered hashtable with a default capacity and load factor, which is 0.75.
      OrderedHashtable​(int initialCapacity)
      Constructs a new, empty ordered hashtable with the specified initial capacity and default load factor, which is 0.75.
      OrderedHashtable​(int initialCapacity, float loadFactor)
      Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
      OrderedHashtable​(java.util.Map<? extends K,​? extends V> t)
      Constructs a new hashtable with the same mappings as the given Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears this hashtable so that it contains neither keys nor values.
      java.lang.Object clone()
      Creates a shallow copy of this hashtable.
      java.util.Enumeration<V> elements()
      Returns an ordered enumeration of the values in this hashtable.
      java.util.List<java.util.Map.Entry<K,​V>> entryList()
      Returns a List view of the entries contained in this Hashtable.
      java.util.Set<K> keySet()
      Returns a view on the set of keys which is backed by the Hashtable
      V put​(K key, V value)
      Maps the specified key to the specified value in this hashtable.
      void putAll​(java.util.Map<? extends K,​? extends V> t)
      Copies all of the mappings from the specified Map to this Hashtable.
      V remove​(java.lang.Object key)
      Removes the key (and its corresponding value) from this hashtable.
      java.util.Collection<V> values()
      Returns a Collection view of the values contained in this Hashtable.
      • Methods inherited from class java.util.Hashtable

        compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, merge, putIfAbsent, rehash, remove, replace, replace, replaceAll, size, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • OrderedHashtable

        public OrderedHashtable()
        Constructs a new, empty ordered hashtable with a default capacity and load factor, which is 0.75.
      • OrderedHashtable

        public OrderedHashtable​(int initialCapacity)
        Constructs a new, empty ordered hashtable with the specified initial capacity and default load factor, which is 0.75.
        Parameters:
        initialCapacity - the initial capacity of the hashtable.
      • OrderedHashtable

        public OrderedHashtable​(int initialCapacity,
                                float loadFactor)
        Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
        Parameters:
        initialCapacity - the initial capacity of the hashtable.
        loadFactor - the load factor of the hashtable.
      • OrderedHashtable

        public OrderedHashtable​(java.util.Map<? extends K,​? extends V> t)
        Constructs a new hashtable with the same mappings as the given Map. The hashtable is created with a capacity of twice the number of entries in the given Map or 11 (whichever is greater), and a default load factor, which is 0.75. If t is an instance of OrderedHashtable the new Hashtable contains its values in the same order as t . Creating a new Hashtable with this constructor is very expensive.
    • Method Detail

      • clear

        public void clear()
        Clears this hashtable so that it contains neither keys nor values.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.Hashtable<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Returns a view on the set of keys which is backed by the Hashtable
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.keySet()
      • clone

        public java.lang.Object clone()
        Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a very expensive operation.
        Overrides:
        clone in class java.util.Hashtable<K,​V>
        Returns:
        an ordered clone of the ordered hashtable.
      • elements

        public java.util.Enumeration<V> elements()
        Returns an ordered enumeration of the values in this hashtable. Use the Enumeration methods on the returned object to fetch the elements sequentially.
        Overrides:
        elements in class java.util.Hashtable<K,​V>
        Returns:
        an enumeration of the values in this hashtable.
      • entryList

        public java.util.List<java.util.Map.Entry<K,​V>> entryList()
        Returns a List view of the entries contained in this Hashtable. Each element in this collection is a Map.Entry. The List is not backed by the Hashtable. The entries are returned in the order they were inserted into the hashtable. This is a very expensive operation.
        Returns:
        an ordered list view of the mappings contained in this map.
        Throws:
        java.lang.NullPointerException - DOCUMENT-ME
      • put

        public V put​(K key,
                     V value)
        Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.

        The value can be retrieved by calling the get method with a key that is equal to the original key. Calls to this method determine the order of the values.

        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.Hashtable<K,​V>
        Parameters:
        key - the hashtable key.
        value - the value.
        Returns:
        the previous value of the specified key in this hashtable, or null if it did not have one.
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> t)
        Copies all of the mappings from the specified Map to this Hashtable. These mappings will replace any mappings that this Hashtable had for any of the keys currently in the specified Map. This method maintains the order of entries as they are returned by t.entrySet() .or t.entryList() if t is an instance of OrderedHashtable.
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Overrides:
        putAll in class java.util.Hashtable<K,​V>
        Parameters:
        t - Mappings to be stored in this map.
        See Also:
        Hashtable.putAll(java.util.Map)
      • remove

        public V remove​(java.lang.Object key)
        Removes the key (and its corresponding value) from this hashtable. This method does nothing if the key is not in the hashtable. This is very expensive operation.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.Hashtable<K,​V>
        Parameters:
        key - the key that needs to be removed.
        Returns:
        the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping.
      • values

        public java.util.Collection<V> values()
        Returns a Collection view of the values contained in this Hashtable. The Collection does not support element removal or addition. The Collection returns the Hashtable's values in the order they were added to it with the put() method.
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.Hashtable<K,​V>
        Returns:
        an ordered collection view of the values contained in this map.