org.apache.commons.collections
Class SequencedHashMap
java.lang.Object
org.apache.commons.collections.SequencedHashMap
- Cloneable, Externalizable, Map
public class SequencedHashMap
extends java.lang.Object
implements Map, Cloneable, Externalizable
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:
java.util.List.remove(Object)
and
java.util.Map.remove(Object)
).
This class is not thread safe. When a thread safe implementation is
required, use
java.util.Collections.synchronizedMap(Map)
as it is documented,
or use explicit synchronization controls.
$Revision: 1.28 $ $Date: 2004/02/18 01:15:42 $- Michael A. Smith
- Daniel Rall
- Henning P. Schmiedehausen
- Stephen Colebourne
SequencedHashMap() - Construct a new sequenced hash map with default initial size and load
factor.
|
SequencedHashMap(Map m) - Construct a new sequenced hash map and add all the elements in the
specified map.
|
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.
|
void | clear() - Implements
Map.clear() .
|
Object | clone() - Creates a shallow copy of this object, preserving the internal structure
by copying only references.
|
boolean | containsKey(Object key) - Implements
Map.containsKey(Object) .
|
boolean | containsValue(Object value) - Implements
Map.containsValue(Object) .
|
Set | entrySet() - Implements
Map.entrySet() .
|
boolean | equals(Object obj) - Implements
Map.equals(Object) .
|
Object | get(Object o) - Implements
Map.get(Object) .
|
Object | get(int index) - Gets the key at the specified index.
|
Map.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.
|
Map.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
Map.hashCode() .
|
int | indexOf(Object key) - Gets the index of the specified key.
|
boolean | isEmpty() - Implements
Map.isEmpty() .
|
Iterator | iterator() - Gets an iterator over the keys.
|
Set | keySet() - Implements
Map.keySet() .
|
int | lastIndexOf(Object key) - Gets the last index of the specified key.
|
Object | put(Object key, Object value) - Implements
Map.put(Object, Object) .
|
void | putAll(Map t) - Adds all the mappings in the specified map to this map, replacing any
mappings that already exist (as per
Map.putAll(Map) ).
|
void | readExternal(ObjectInput in) - Deserializes this map from the given stream.
|
Object | remove(Object key) - Implements
Map.remove(Object) .
|
Object | remove(int index) - Removes the element at the specified index.
|
List | sequence() - Returns a List view of the keys rather than a set view.
|
int | size() - Implements
Map.size() .
|
String | toString() - Provides a string representation of the entries within the map.
|
Collection | values() - Implements
Map.values() .
|
void | writeExternal(ObjectOutput out) - Serializes this map to the given stream.
|
SequencedHashMap
public SequencedHashMap()
Construct a new sequenced hash map with default initial size and load
factor.
SequencedHashMap
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)
.
SequencedHashMap
public SequencedHashMap(int initialSize)
Construct a new sequenced hash map with the specified initial size and
default load factor.
initialSize
- the initial size for the hash table
SequencedHashMap
public SequencedHashMap(int initialSize,
float loadFactor)
Construct a new sequenced hash map with the specified initial size and
load factor.
initialSize
- the initial size for the hash tableloadFactor
- the load factor for the hash table.
HashMap.HashMap(int,float)
clear
public void clear()
Implements Map.clear()
.
clone
public Object clone()
throws CloneNotSupportedException
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.
- A clone of this instance.
containsKey
public boolean containsKey(Object key)
Implements Map.containsKey(Object)
.
containsValue
public boolean containsValue(Object value)
Implements Map.containsValue(Object)
.
entrySet
public Set entrySet()
Implements Map.entrySet()
.
equals
public boolean equals(Object obj)
Implements Map.equals(Object)
.
get
public Object get(Object o)
Implements Map.get(Object)
.
get
public Object get(int index)
Gets the key at the specified index.
index
- the index to retrieve
- the key at the specified index, or null
getFirst
public Map.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.
- The first entry in the sequence, or
null
if the
map is empty.
getFirstKey
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.
- The first key in the sequence, or
null
if the
map is empty.
getFirstValue
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.
- The first value in the sequence, or
null
if the
map is empty.
getLast
public Map.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).
- The last entry in the sequence, or
null
if the map
is empty.
getLastKey
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.
- The last key in the sequence, or
null
if the map is
empty.
getLastValue
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.
- The last value in the sequence, or
null
if the map
is empty.
getValue
public Object getValue(int index)
Gets the value at the specified index.
index
- the index to retrieve
- the value at the specified index, or null
hashCode
public int hashCode()
Implements Map.hashCode()
.
indexOf
public int indexOf(Object key)
Gets the index of the specified key.
key
- the key to find the index of
- the index, or -1 if not found
isEmpty
public boolean isEmpty()
Implements Map.isEmpty()
.
iterator
public Iterator iterator()
Gets an iterator over the keys.
- an iterator over the keys
keySet
public Set keySet()
Implements Map.keySet()
.
lastIndexOf
public int lastIndexOf(Object key)
Gets the last index of the specified key.
key
- the key to find the index of
- the index, or -1 if not found
put
public Object put(Object key,
Object value)
Implements Map.put(Object, Object)
.
putAll
public void putAll(Map t)
Adds all the mappings in the specified map to this map, replacing any
mappings that already exist (as per Map.putAll(Map)
). The order
in which the entries are added is determined by the iterator returned
from Map.entrySet()
for the specified map.
t
- the mappings that should be added to this map.
readExternal
public void readExternal(ObjectInput in)
throws IOException,
ClassNotFoundException
Deserializes this map from the given stream.
in
- the stream to deserialize from
remove
public Object remove(Object key)
Implements Map.remove(Object)
.
remove
public Object remove(int index)
Removes the element at the specified index.
index
- The index of the object to remove.
- The previous value corresponding the
key
, or
null
if none existed.
sequence
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
java.util.ListIterator.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()
- The ordered list of keys.
size
public int size()
Implements Map.size()
.
toString
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.
values
public Collection values()
Implements Map.values()
.
writeExternal
public void writeExternal(ObjectOutput out)
throws IOException
Serializes this map to the given stream.
out
- the stream to serialize to
Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.