com.foxsmart.ic.lang
Class OrderedHashMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by com.foxsmart.ic.lang.OrderedHashMap
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class OrderedHashMap
extends java.util.HashMap

This class provides the functionality of a Hash Map while maintaining the order of entered items.

See Also:
Serialized Form

Constructor Summary
OrderedHashMap()
          Constructs a new, empty OrderedHashMap with the JDK default capacity and load factor.
OrderedHashMap(int initialCapacity)
          Constructs a new, empty OrderedHashMap with the specified initial capacity and the default JDK load factor.
OrderedHashMap(int initialCapacity, float loadFactor)
          Constructs a new, empty OrderedHashMap with the specified initial capacity and the specified load factor.
OrderedHashMap(java.util.Map map)
          Constructs a new OrderedHashMap with the same mappings as the given map.
 
Method Summary
 void clear()
          Removes all mappings from this map.
 java.lang.Object clone()
          Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
 java.util.List keyList()
          Returns the list of keys in the order they were entered into the hash map.
static void main(java.lang.String[] args)
          Test Driver.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map map)
          Copies all of the mappings from the specified map to this one.
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for this key from this map if present.
 int size()
          Returns the number of entries in the hash map.
 java.util.Collection values()
          Returns an ordered collection of the values contained in this map.
 
Methods inherited from class java.util.HashMap
containsKey, containsValue, entrySet, get, isEmpty, keySet
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

OrderedHashMap

public OrderedHashMap()
Constructs a new, empty OrderedHashMap with the JDK default capacity and load factor.


OrderedHashMap

public OrderedHashMap(int initialCapacity)
Constructs a new, empty OrderedHashMap with the specified initial capacity and the default JDK load factor.

Parameters:
initialCapacity - The initial capacity of the OrderedHashMap

OrderedHashMap

public OrderedHashMap(int initialCapacity,
                      float loadFactor)
Constructs a new, empty OrderedHashMap with the specified initial capacity and the specified load factor.

Parameters:
initialCapacity - The initial capacity of the OrderedHashMap
loadFactor - The initial load factor of the OrderedHashMap

OrderedHashMap

public OrderedHashMap(java.util.Map map)
Constructs a new OrderedHashMap with the same mappings as the given map.

Parameters:
map - The map to initialize the OrderedHashMap with
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

In terms of order, the following rules apply:

1) If a new value is being entered and there is no existing value, then the value is added to the end of the array list.

2) If a new value is being entered which is replacing an existing value, then the array list is left alone and the order is unchanged (i.e. the order is based on the originally inserted value).

3) If a new value is being entered as null and there is an existing value, then the value is removed from the array list order. This is useful if an entry needs to replace an existing entry and the order should be updated. Simply replace the original value with null and then re-add the new value.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
Parameters:
key - Key with which the specified vlaue is to be associated.
value - Value to be associated with the specified key.
Returns:
Previous value associated with specified key, or null if there was no mapping for a key. A null return can also indicate that the HashMap previously associated null with the specified key.

putAll

public void putAll(java.util.Map map)
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified Map.

Specified by:
putAll in interface java.util.Map
Overrides:
putAll in class java.util.HashMap
Parameters:
map - Mappings to be stored in this map

clear

public void clear()
Removes all mappings from this map.

Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.HashMap

remove

public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from this map if present.

Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.HashMap
Parameters:
key - Key whose mapping is to be removed from the map.
Returns:
Previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.

values

public java.util.Collection values()
Returns an ordered collection of the values contained in this map.

Specified by:
values in interface java.util.Map
Overrides:
values in class java.util.HashMap
Returns:
The ordered collection of the values contained in this map.

keyList

public java.util.List keyList()
Returns the list of keys in the order they were entered into the hash map.

Returns:
the key list.

size

public int size()
Returns the number of entries in the hash map.

Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.HashMap
Returns:
the number of entries.

clone

public java.lang.Object clone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

Overrides:
clone in class java.util.HashMap
Returns:
a shallow copy of this map.

main

public static void main(java.lang.String[] args)
Test Driver.

Parameters:
args - the arguments


Copyright © 2005-2008 Fox Smart, Inc. All Rights Reserved.