Package org.apache.axis.collections
Class SequencedHashMap
java.lang.Object
org.apache.axis.collections.SequencedHashMap
- All Implemented Interfaces:
Externalizable
,Serializable
,Cloneable
,Map
- Direct Known Subclasses:
LRUMap
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 List
because of incompatible interface definitions. The remove
methods in List and Map have different return values (see: List.remove(Object)
and Map.remove(Object)
).
This class is not thread safe. When a thread safe implementation is
required, use Collections.synchronizedMap(Map)
as it is documented,
or use explicit synchronization controls.
- Since:
- Commons Collections 2.0
- Author:
- Michael A. Smith, Daniel Rall, Henning P. Schmiedehausen, Stephen Colebourne
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct 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.Construct a new sequenced hash map and add all the elements in the specified map. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
ImplementsMap.clear()
.clone()
Creates a shallow copy of this object, preserving the internal structure by copying only references.boolean
containsKey
(Object key) ImplementsMap.containsKey(Object)
.boolean
containsValue
(Object value) ImplementsMap.containsValue(Object)
.entrySet()
ImplementsMap.entrySet()
.boolean
ImplementsMap.equals(Object)
.get
(int index) Gets the key at the specified index.ImplementsMap.get(Object)
.getFirst()
Return the entry for the "oldest" mapping.Return the key for the "oldest" mapping.Return the value for the "oldest" mapping.getLast()
Return the entry for the "newest" mapping.Return the key for the "newest" mapping.Return the value for the "newest" mapping.getValue
(int index) Gets the value at the specified index.int
hashCode()
ImplementsMap.hashCode()
.int
Gets the index of the specified key.boolean
isEmpty()
ImplementsMap.isEmpty()
.iterator()
Gets an iterator over the keys.keySet()
ImplementsMap.keySet()
.int
lastIndexOf
(Object key) Gets the last index of the specified key.ImplementsMap.put(Object, Object)
.void
Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as perMap.putAll(Map)
).void
Deserializes this map from the given stream.remove
(int index) Removes the element at the specified index.ImplementsMap.remove(Object)
.sequence()
Returns a List view of the keys rather than a set view.int
size()
ImplementsMap.size()
.toString()
Provides a string representation of the entries within the map.values()
ImplementsMap.values()
.void
Serializes this map to the given stream.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
SequencedHashMap
public SequencedHashMap()Construct a new sequenced hash map with default initial size and load factor. -
SequencedHashMap
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:
-
SequencedHashMap
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 tableloadFactor
- the load factor for the hash table.- See Also:
-
SequencedHashMap
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 byputAll(Map)
.
-
-
Method Details
-
size
public int size()ImplementsMap.size()
. -
isEmpty
public boolean isEmpty()ImplementsMap.isEmpty()
. -
containsKey
ImplementsMap.containsKey(Object)
.- Specified by:
containsKey
in interfaceMap
-
containsValue
ImplementsMap.containsValue(Object)
.- Specified by:
containsValue
in interfaceMap
-
get
ImplementsMap.get(Object)
. -
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 usingentrySet().iterator().next()
, but this method provides an optimized implementation.- Returns:
- The first entry in the sequence, or
null
if the map is empty.
-
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 usinggetFirst().getKey()
, but this method provides a slightly optimized implementation.- Returns:
- The first key in the sequence, or
null
if the map is empty.
-
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 usinggetFirst().getValue()
, but this method provides a slightly optimized implementation.- Returns:
- The first value in the sequence, or
null
if the map is empty.
-
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.
-
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 usinggetLast().getKey()
, but this method provides a slightly optimized implementation.- Returns:
- The last key in the sequence, or
null
if the map is empty.
-
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 usinggetLast().getValue()
, but this method provides a slightly optimized implementation.- Returns:
- The last value in the sequence, or
null
if the map is empty.
-
put
ImplementsMap.put(Object, Object)
. -
remove
ImplementsMap.remove(Object)
. -
putAll
Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as perMap.putAll(Map)
). The order in which the entries are added is determined by the iterator returned fromMap.entrySet()
for the specified map.- Specified by:
putAll
in interfaceMap
- Parameters:
t
- the mappings that should be added to this map.- Throws:
NullPointerException
- ift
isnull
-
clear
public void clear()ImplementsMap.clear()
. -
equals
ImplementsMap.equals(Object)
. -
hashCode
public int hashCode()ImplementsMap.hashCode()
. -
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, useentrySet()
.iterator()
and iterate over the entries in the map formatting them as appropriate. -
keySet
ImplementsMap.keySet()
. -
values
ImplementsMap.values()
. -
entrySet
ImplementsMap.entrySet()
. -
clone
Creates a shallow copy of this object, preserving the internal structure by copying only references. The keys and values themselves are notclone()
'd. The cloned object maintains the same sequence.- Overrides:
clone
in classObject
- Returns:
- A clone of this instance.
- Throws:
CloneNotSupportedException
- if clone is not supported by a subclass.
-
get
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 theindex
is< 0
or>
the size of the map.
-
getValue
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 theindex
is< 0
or>
the size of the map.
-
indexOf
Gets the index of the specified key.- Parameters:
key
- the key to find the index of- Returns:
- the index, or -1 if not found
-
iterator
Gets an iterator over the keys.- Returns:
- an iterator over the keys
-
lastIndexOf
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
-
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 (usingListIterator.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:
-
remove
Removes the element at the specified index.- Parameters:
index
- The index of the object to remove.- Returns:
- The previous value coressponding the
key
, ornull
if none existed. - Throws:
ArrayIndexOutOfBoundsException
- if theindex
is< 0
or>
the size of the map.
-
readExternal
Deserializes this map from the given stream.- Specified by:
readExternal
in interfaceExternalizable
- Parameters:
in
- the stream to deserialize from- Throws:
IOException
- if the stream raises itClassNotFoundException
- if the stream raises it
-
writeExternal
Serializes this map to the given stream.- Specified by:
writeExternal
in interfaceExternalizable
- Parameters:
out
- the stream to serialize to- Throws:
IOException
- if the stream raises it
-