com.livis.flabes.util
Class LinkedHashMap

java.lang.Object
  |
  +--java.util.AbstractMap
        |
        +--com.livis.flabes.util.LinkedHashMap

public class LinkedHashMap
extends java.util.AbstractMap
implements java.util.Map, java.io.Serializable

Hash table based implementation of the Map interface. This implementation provides all of the optional Map operations, and permits null values and the null key. (HashMap is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) In addition, elements in the map are ordered and doubly linked together.

This implementation provides constant-time performance for the basic operations (get and put), assuming the the hash function disperses the elements properly among the buckets. Iteration over Collection views requires time proportional to its size (the number of key-value mappings) and returns elements in the order they are linked. In a HashMap the iteration would require time proportional to the capacity of the map plus the map size.

An instance of LinkedHashMap has two parameters that affect its efficiency: its capacity and its load factor. The load factor should be between 0.0 and 1.0. When the number of mappings in the LinkedHashMap exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method which requires time proportional to the number of key-value mappings in the map. Larger load factors use memory more efficiently, at the expense of larger expected time per lookup.

If many mappings are to be stored in a LinkedHashMap, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Note that this implementation is not synchronized. If multiple threads access a LinkedHashMap concurrently, and at least one of the threads modifies the LinkedHashMap structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that is already contained in the Table is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the LinkedHashMap. If no such object exists, the LinkedHashMap should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the LinkedHashMap:

	Map m = Collections.synchronizedMap(new LinkedHashMap(...));
 

The Iterators returned by the iterator methods of the Collections returned by all of LinkedHashMap's "collection view methods" are fail-fast: if the LinkedHashMap is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Since:
JDK1.2
See Also:
Object.hashCode(), Collection, Map, TreeMap, Hashtable, HashMap, Serialized Form

Inner classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
LinkedHashMap()
          Constructs a new, empty LinkedHashMap with a default capacity and load factor.
LinkedHashMap(int initialCapacity)
          Constructs a new, empty LinkedHashMap with the specified initial capacity and default load factor.
LinkedHashMap(int initialCapacity, float loadFactor)
          Constructs a new, empty LinkedHashMap with the specified initial capacity and the specified load factor.
LinkedHashMap(java.util.Map t)
          Constructs a new LinkedHashMap with the same mappings as the given Map.
 
Method Summary
 void clear()
          Removes all mappings from this LinkedHashMap.
 java.lang.Object clone()
          Returns a shallow copy of this LinkedHashMap.
 boolean containsKey(java.lang.Object key)
          Returns true if this LinkedHashMap contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this LinkedHashMap maps one or more keys to the specified value.
 java.util.Set entrySet()
          Returns a Collection view of the mappings contained in this LinkedHashMap.
 boolean equals(java.lang.Object o)
          Compares the specified Object with this Map for equality.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which this LinkedHashMap maps the specified key.
 int indexOf(java.lang.Object key)
          Returns the position of the mapping for the specified key in the ordered map.
 boolean isEmpty()
          Returns true if this Map contains no key-value mappings.
 java.util.Set keySet()
          Returns a Set view of the keys contained in this LinkedHashMap.
 java.lang.Object put(int index, java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this LinkedHashMap and position the mapping at the specified index.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this LinkedHashMap.
 void putAll(java.util.Map t)
          Copies all of the mappings from the specified Map to this LinkedHashMap These mappings will replace any mappings that this LinkedHashMap had for any of the keys currently in the specified Map.
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for this key from this LinkedHashMap if present.
 int size()
          Returns the number of key-value mappings in this Map.
 java.util.Collection values()
          Returns a Collection view of the values contained in this LinkedHashMap.
 
Methods inherited from class java.util.AbstractMap
hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinkedHashMap

public LinkedHashMap(int initialCapacity,
                     float loadFactor)
Constructs a new, empty LinkedHashMap with the specified initial capacity and the specified load factor.
Parameters:
initialCapacity - the initial capacity of the LinkedHashMap.
loadFactor - a number between 0.0 and 1.0.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero.

LinkedHashMap

public LinkedHashMap(int initialCapacity)
Constructs a new, empty LinkedHashMap with the specified initial capacity and default load factor.
Parameters:
initialCapacity - the initial capacity of the LinkedHashMap.

LinkedHashMap

public LinkedHashMap()
Constructs a new, empty LinkedHashMap with a default capacity and load factor.

LinkedHashMap

public LinkedHashMap(java.util.Map t)
Constructs a new LinkedHashMap with the same mappings as the given Map. The LinkedHashMap is created with a capacity of thrice the number of mappings in the given Map or 11 (whichever is greater), and a default load factor.
Method Detail

size

public int size()
Returns the number of key-value mappings in this Map.
Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap

isEmpty

public boolean isEmpty()
Returns true if this Map contains no key-value mappings.
Specified by:
isEmpty in interface java.util.Map
Overrides:
isEmpty in class java.util.AbstractMap

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this LinkedHashMap maps one or more keys to the specified value.
Specified by:
containsValue in interface java.util.Map
Overrides:
containsValue in class java.util.AbstractMap
Parameters:
value - value whose presence in this Map is to be tested.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this LinkedHashMap contains a mapping for the specified key.
Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap
Parameters:
key - key whose presence in this Map is to be tested.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which this LinkedHashMap maps the specified key. Returns null if the LinkedHashMap contains no mapping for this key. A return value of null does not necessarily indicate that the LinkedHashMap contains no mapping for the key; it's also possible that the LinkedHashMap explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.
Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap
Parameters:
key - key whose associated value is to be returned.

indexOf

public int indexOf(java.lang.Object key)
Returns the position of the mapping for the specified key in the ordered map.
Parameters:
key - the specified key.
Returns:
index of the key mapping.

put

public java.lang.Object put(int index,
                            java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key in this LinkedHashMap and position the mapping at the specified index. If the LinkedHashMap previously contained a mapping for this key, the old value is replaced and the position of this mapping entry in the double linked list remains the same. Otherwise, a new mapping entry is created and inserted into the list at the specified position.
Parameters:
index - the position to put the key-value mapping.
key - key with which the specified value 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 key. A null return can also indicate that the LinkedHashMap previously associated null with the specified key.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key in this LinkedHashMap. If the LinkedHashMap previously contained a mapping for this key, the old value is replaced. The mapping entry is also appended to the end of the ordered linked list.
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap
Parameters:
key - key with which the specified value 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 key. A null return can also indicate that the LinkedHashMap previously associated null with the specified key.

remove

public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from this LinkedHashMap if present. The mapping would also be removed from the double linked list.
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap
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 LinkedHashMap previously associated null with the specified key.

putAll

public void putAll(java.util.Map t)
Copies all of the mappings from the specified Map to this LinkedHashMap These mappings will replace any mappings that this LinkedHashMap 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.AbstractMap
Parameters:
t - Mappings to be stored in this Map.

clear

public void clear()
Removes all mappings from this LinkedHashMap.
Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.AbstractMap

clone

public java.lang.Object clone()
Returns a shallow copy of this LinkedHashMap. The keys and values themselves are not cloned.
Overrides:
clone in class java.lang.Object

keySet

public java.util.Set keySet()
Returns a Set view of the keys contained in this LinkedHashMap. The Set is backed by the LinkedHashMap, so changes to the LinkedHashMap are reflected in the Set, and vice-versa. The Set supports element removal, which removes the corresponding mapping from the LinkedHashMap, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations.
Specified by:
keySet in interface java.util.Map
Overrides:
keySet in class java.util.AbstractMap

values

public java.util.Collection values()
Returns a Collection view of the values contained in this LinkedHashMap. The Collection is backed by the LinkedHashMap, so changes to the LinkedHashMap are reflected in the Collection, and vice-versa. The Collection supports element removal, which removes the corresponding mapping from the LinkedHashMap, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
Specified by:
values in interface java.util.Map
Overrides:
values in class java.util.AbstractMap

entrySet

public java.util.Set entrySet()
Returns a Collection view of the mappings contained in this LinkedHashMap. Each element in the returned collection is a Map.Entry. The Collection is backed by the LinkedHashMap, so changes to the LinkedHashMap are reflected in the Collection, and vice-versa. The Collection supports element removal, which removes the corresponding mapping from the LinkedHashMap, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
Specified by:
entrySet in interface java.util.Map
Overrides:
entrySet in class java.util.AbstractMap
See Also:
Map.Entry

equals

public boolean equals(java.lang.Object o)
Compares the specified Object with this Map for equality. Returns true if the given object is also a LinkedHashMap and the two Maps represent the same mappings in the same order. More formally, two Maps t1 and t2 represent the same mappings if t1.keySet().equals(t2.keySet()) and for every key k in t1.keySet(), (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k))) .

This implementation first checks if the specified Object is this Map; if so it returns true. Then, it checks if the specified Object is a Map whose size is identical to the size of this Set; if not, it it returns false. If so, it iterates over this Map and the specified Map's entrySet() Collection, and checks that the specified Map contains each mapping that this Map contains at the same position. If the specified Map fails to contain such a mapping in the right order, false is returned. If the iteration completes, true is returned.

Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.util.AbstractMap
Parameters:
o - Object to be compared for equality with this Map.
Returns:
true if the specified Object is equal to this Map.