net.sourceforge.jaulp.collections
Class InsertionOrderMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<K,V>
              extended by net.sourceforge.jaulp.collections.InsertionOrderMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>

public class InsertionOrderMap<K,V>
extends java.util.LinkedHashMap<K,V>

This class overwrites the put-method from the LinkedHashMap. That inserts the value to the right order it was inserted in the Map. Note that the difference in the LinkedHashMap is the order does not change if we put the same key with a new value. In this class the order changes when we put a new value with the same key.

Version:
1.0
Author:
Asterios Raptis
See Also:
LinkedHashMap, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
InsertionOrderMap()
          Constructs an empty insertion-ordered InsertionOrderMap instance with a default capacity (16) and load factor (0.75).
InsertionOrderMap(int initialCapacity)
          Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and a default load factor (0.75).
InsertionOrderMap(int initialCapacity, float loadFactor)
          Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and load factor.
InsertionOrderMap(int initialCapacity, float loadFactor, boolean accessOrder)
          Constructs an empty InsertionOrderMap instance with the specified initial capacity, load factor and ordering mode.
InsertionOrderMap(java.util.Map<? extends K,? extends V> m)
          Constructs an insertion-ordered InsertionOrderMap instance with the same mappings as the specified map.
 
Method Summary
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, putAll, remove, size, values
 
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
containsKey, entrySet, equals, hashCode, isEmpty, keySet, putAll, remove, size, values
 

Constructor Detail

InsertionOrderMap

public InsertionOrderMap()
Constructs an empty insertion-ordered InsertionOrderMap instance with a default capacity (16) and load factor (0.75).


InsertionOrderMap

public InsertionOrderMap(int initialCapacity)
Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and a default load factor (0.75).

Parameters:
initialCapacity - the initial capacity.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative.

InsertionOrderMap

public InsertionOrderMap(int initialCapacity,
                         float loadFactor)
Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and load factor.

Parameters:
initialCapacity - the initial capacity.
loadFactor - the load factor.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.

InsertionOrderMap

public InsertionOrderMap(int initialCapacity,
                         float loadFactor,
                         boolean accessOrder)
Constructs an empty InsertionOrderMap instance with the specified initial capacity, load factor and ordering mode.

Parameters:
initialCapacity - the initial capacity.
loadFactor - the load factor.
accessOrder - the ordering mode - true for access-order, false for insertion-order.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.

InsertionOrderMap

public InsertionOrderMap(java.util.Map<? extends K,? extends V> m)
Constructs an insertion-ordered InsertionOrderMap instance with the same mappings as the specified map. The InsertionOrderMap instance is created with a a default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map.

Parameters:
m - the map whose mappings are to be placed in this map.
Throws:
java.lang.NullPointerException - if the specified map is null.
Method Detail

put

public V put(K key,
             V 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 removed and then insert new with the same key and the new value. That inserts the value to the right order it was inserted in the Map. Note that the difference in the LinkedHashMap is the order does not change if we put the same key with a new value. In this class the order changes when we put a new value with the same key.

Specified by:
put in interface java.util.Map<K,V>
Overrides:
put in class java.util.HashMap<K,V>
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 HashMap previously associated null with the specified key.


Copyright © 2010. All Rights Reserved.