public class

SequencedHashMap

extends Object
implements Externalizable Cloneable Map<K, V>
java.lang.Object
   ↳ org.apache.commons.collections.SequencedHashMap
Known Direct Subclasses

This class is deprecated.
Replaced by LinkedMap and ListOrderedMap in map subpackage. Due to be removed in v4.0.

Class Overview

A map of objects whose mapping entries are sequenced based on the order in which they were added. This data structure has fast O(1) search time, deletion time, and insertion time.

Although this map is sequenced, it cannot implement java.util.List because of incompatible interface definitions. The remove methods in List and Map have different return values (see: remove(Object) and remove(Object)).

This class is not thread safe. When a thread safe implementation is required, use synchronizedMap(Map) as it is documented, or use explicit synchronization controls.

Summary

Public Constructors
SequencedHashMap()
Construct a new sequenced hash map with default initial size and load factor.
SequencedHashMap(int initialSize)
Construct a new sequenced hash map with the specified initial size and default load factor.
SequencedHashMap(int initialSize, float loadFactor)
Construct a new sequenced hash map with the specified initial size and load factor.
SequencedHashMap(Map m)
Construct a new sequenced hash map and add all the elements in the specified map.
Public Methods
void clear()
Implements clear().
Object clone()
Creates a shallow copy of this object, preserving the internal structure by copying only references.
boolean containsKey(Object key)
boolean containsValue(Object value)
Set entrySet()
Implements entrySet().
boolean equals(Object obj)
Implements equals(Object).
Object get(int index)
Gets the key at the specified index.
Object get(Object o)
Implements get(Object).
Entry getFirst()
Return the entry for the "oldest" mapping.
Object getFirstKey()
Return the key for the "oldest" mapping.
Object getFirstValue()
Return the value for the "oldest" mapping.
Entry getLast()
Return the entry for the "newest" mapping.
Object getLastKey()
Return the key for the "newest" mapping.
Object getLastValue()
Return the value for the "newest" mapping.
Object getValue(int index)
Gets the value at the specified index.
int hashCode()
Implements hashCode().
int indexOf(Object key)
Gets the index of the specified key.
boolean isEmpty()
Implements isEmpty().
Iterator iterator()
Gets an iterator over the keys.
Set keySet()
Implements keySet().
int lastIndexOf(Object key)
Gets the last index of the specified key.
Object put(Object key, Object value)
void putAll(Map t)
Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as per putAll(Map)).
void readExternal(ObjectInput in)
Deserializes this map from the given stream.
Object remove(int index)
Removes the element at the specified index.
Object remove(Object key)
Implements remove(Object).
List sequence()
Returns a List view of the keys rather than a set view.
int size()
Implements size().
String toString()
Provides a string representation of the entries within the map.
Collection values()
Implements values().
void writeExternal(ObjectOutput out)
Serializes this map to the given stream.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.io.Externalizable
From interface java.util.Map

Public Constructors

public SequencedHashMap ()

Construct a new sequenced hash map with default initial size and load factor.

public SequencedHashMap (int initialSize)

Construct a new sequenced hash map with the specified initial size and default load factor.

Parameters
initialSize the initial size for the hash table
See Also

public SequencedHashMap (int initialSize, float loadFactor)

Construct a new sequenced hash map with the specified initial size and load factor.

Parameters
initialSize the initial size for the hash table
loadFactor the load factor for the hash table.

public SequencedHashMap (Map m)

Construct a new sequenced hash map and add all the elements in the specified map. The order in which the mappings in the specified map are added is defined by putAll(Map).

Public Methods

public void clear ()

Implements clear().

public Object clone ()

Creates a shallow copy of this object, preserving the internal structure by copying only references. The keys and values themselves are not clone()'d. The cloned object maintains the same sequence.

Returns
  • A clone of this instance.
Throws
CloneNotSupportedException if clone is not supported by a subclass.

public boolean containsKey (Object key)

Implements containsKey(Object).

public boolean containsValue (Object value)

public Set entrySet ()

Implements entrySet().

public boolean equals (Object obj)

Implements equals(Object).

public Object get (int index)

Gets the key at the specified index.

Parameters
index the index to retrieve
Returns
  • the key at the specified index, or null
Throws
ArrayIndexOutOfBoundsException if the index is < 0 or > the size of the map.

public Object get (Object o)

Implements get(Object).

public Entry getFirst ()

Return the entry for the "oldest" mapping. That is, return the Map.Entry for the key-value pair that was first put into the map when compared to all the other pairings in the map. This behavior is equivalent to using entrySet().iterator().next(), but this method provides an optimized implementation.

Returns
  • The first entry in the sequence, or null if the map is empty.

public Object getFirstKey ()

Return the key for the "oldest" mapping. That is, return the key for the mapping that was first put into the map when compared to all the other objects in the map. This behavior is equivalent to using getFirst().getKey(), but this method provides a slightly optimized implementation.

Returns
  • The first key in the sequence, or null if the map is empty.

public Object getFirstValue ()

Return the value for the "oldest" mapping. That is, return the value for the mapping that was first put into the map when compared to all the other objects in the map. This behavior is equivalent to using getFirst().getValue(), but this method provides a slightly optimized implementation.

Returns
  • The first value in the sequence, or null if the map is empty.

public Entry getLast ()

Return the entry for the "newest" mapping. That is, return the Map.Entry for the key-value pair that was first put into the map when compared to all the other pairings in the map. The behavior is equivalent to:

    Object obj = null;
    Iterator iter = entrySet().iterator();
    while(iter.hasNext()) {
      obj = iter.next();
    }
    return (Map.Entry)obj;
  
However, the implementation of this method ensures an O(1) lookup of the last key rather than O(n).

Returns
  • The last entry in the sequence, or null if the map is empty.

public Object getLastKey ()

Return the key for the "newest" mapping. That is, return the key for the mapping that was last put into the map when compared to all the other objects in the map. This behavior is equivalent to using getLast().getKey(), but this method provides a slightly optimized implementation.

Returns
  • The last key in the sequence, or null if the map is empty.

public Object getLastValue ()

Return the value for the "newest" mapping. That is, return the value for the mapping that was last put into the map when compared to all the other objects in the map. This behavior is equivalent to using getLast().getValue(), but this method provides a slightly optimized implementation.

Returns
  • The last value in the sequence, or null if the map is empty.

public Object getValue (int index)

Gets the value at the specified index.

Parameters
index the index to retrieve
Returns
  • the value at the specified index, or null
Throws
ArrayIndexOutOfBoundsException if the index is < 0 or > the size of the map.

public int hashCode ()

Implements hashCode().

public int indexOf (Object key)

Gets the index of the specified key.

Parameters
key the key to find the index of
Returns
  • the index, or -1 if not found

public boolean isEmpty ()

Implements isEmpty().

public Iterator iterator ()

Gets an iterator over the keys.

Returns
  • an iterator over the keys

public Set keySet ()

Implements keySet().

public int lastIndexOf (Object key)

Gets the last index of the specified key.

Parameters
key the key to find the index of
Returns
  • the index, or -1 if not found

public Object put (Object key, Object value)

Implements put(Object, Object).

public void putAll (Map t)

Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as per putAll(Map)). The order in which the entries are added is determined by the iterator returned from entrySet() for the specified map.

Parameters
t the mappings that should be added to this map.
Throws
NullPointerException if t is null

public void readExternal (ObjectInput in)

Deserializes this map from the given stream.

Parameters
in the stream to deserialize from
Throws
IOException if the stream raises it
ClassNotFoundException if the stream raises it

public Object remove (int index)

Removes the element at the specified index.

Parameters
index The index of the object to remove.
Returns
  • The previous value corresponding the key, or null if none existed.
Throws
ArrayIndexOutOfBoundsException if the index is < 0 or > the size of the map.

public Object remove (Object key)

Implements remove(Object).

public List sequence ()

Returns a List view of the keys rather than a set view. The returned list is unmodifiable. This is required because changes to the values of the list (using set(Object)) will effectively remove the value from the list and reinsert that value at the end of the list, which is an unexpected side effect of changing the value of a list. This occurs because changing the key, changes when the mapping is added to the map and thus where it appears in the list.

An alternative to this method is to use keySet()

Returns
  • The ordered list of keys.
See Also

public int size ()

Implements size().

public String toString ()

Provides a string representation of the entries within the map. The format of the returned string may change with different releases, so this method is suitable for debugging purposes only. If a specific format is required, use entrySet().iterator() and iterate over the entries in the map formatting them as appropriate.

public Collection values ()

Implements values().

public void writeExternal (ObjectOutput out)

Serializes this map to the given stream.

Parameters
out the stream to serialize to
Throws
IOException if the stream raises it