public class SequencedHashMap<K,V> extends Object implements Map<K,V>, Cloneable, Externalizable
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.
Constructor and Description |
---|
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<? extends K,? extends V> m)
Construct a new sequenced hash map and add all the elements in the
specified map.
|
Modifier and Type | Method and Description |
---|---|
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<Map.Entry<K,V>> |
entrySet()
Implements
Map.entrySet() . |
boolean |
equals(Object obj)
Implements
Map.equals(Object) . |
Object |
get(int index)
Returns the key at the specified index.
|
V |
get(Object o)
Implements
Map.get(Object) . |
Map.Entry<K,V> |
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<K,V> |
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)
Returns the value at the specified index.
|
int |
hashCode()
Implements
Map.hashCode() . |
int |
indexOf(Object key)
Retruns the index of the specified key.
|
boolean |
isEmpty()
Implements
Map.isEmpty() . |
Iterator<K> |
iterator()
Returns an iterator over the keys.
|
Set<K> |
keySet()
Implements
Map.keySet() . |
int |
lastIndexOf(Object key)
Returns the last index of the specified key.
|
V |
put(K key,
V value)
Implements
Map.put(Object, Object) . |
void |
putAll(Map<? extends K,? extends V> 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(int index)
Removes the element at the specified index.
|
V |
remove(Object key)
Implements
Map.remove(Object) . |
List<K> |
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.
|
V |
touch(Object key) |
Collection<V> |
values()
Implements
Map.values() . |
void |
writeExternal(ObjectOutput out)
Serializes this map to the given stream.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public SequencedHashMap()
public SequencedHashMap(int initialSize)
initialSize
- the initial size for the hash tableHashMap(int)
public SequencedHashMap(int initialSize, float loadFactor)
initialSize
- the initial size for the hash tableloadFactor
- the load factor for the hash table.HashMap(int,float)
public SequencedHashMap(Map<? extends K,? extends V> m)
putAll(Map)
.m
- The Map
which contains the elements to be copied.public int size()
Map.size()
.public boolean isEmpty()
Map.isEmpty()
.public boolean containsKey(Object key)
Map.containsKey(Object)
.containsKey
in interface Map<K,V>
public boolean containsValue(Object value)
Map.containsValue(Object)
.containsValue
in interface Map<K,V>
public Map.Entry<K,V> getFirst()
entrySet().iterator().next()
, but this method provides an
optimized implementation.null
if the map
is empty.public Object getFirstKey()
getFirst().getKey()
, but this method provides a slightly
optimized implementation.null
if the map is
empty.public Object getFirstValue()
getFirst().getValue()
, but this method provides a slightly
optimized implementation.null
if the map
is empty.public Map.Entry<K,V> getLast()
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).
null
if the map
is empty.public Object getLastKey()
getLast().getKey()
, but this method provides a slightly
optimized implementation.null
if the map is
empty.public Object getLastValue()
getLast().getValue()
, but this method provides a slightly
optimized implementation.null
if the map
is empty.public V put(K key, V value)
Map.put(Object, Object)
.public V remove(Object key)
Map.remove(Object)
.public void putAll(Map<? extends K,? extends V> t)
Map.putAll(Map)
). The order in
which the entries are added is determined by the iterator returned from
Map.entrySet()
for the specified map.putAll
in interface Map<K,V>
t
- the mappings that should be added to this map.NullPointerException
- if t
is null
public void clear()
Map.clear()
.public boolean equals(Object obj)
Map.equals(Object)
.public int hashCode()
Map.hashCode()
.public String toString()
entrySet()
.iterator()
and iterate
over the entries in the map formatting them as appropriate.public Collection<V> values()
Map.values()
.public Set<Map.Entry<K,V>> entrySet()
Map.entrySet()
.public Object clone() throws CloneNotSupportedException
clone()
'd. The cloned object maintains the same sequence.clone
in class Object
CloneNotSupportedException
- if clone is not supported by a
subclass.public Object get(int index)
index
- the index to retrieveArrayIndexOutOfBoundsException
- if the index
is
< 0
or >
the size of the map.public Object getValue(int index)
index
- the index to retrieveArrayIndexOutOfBoundsException
- if the index
is
< 0
or >
the size of the map.public int indexOf(Object key)
key
- the key to find the index ofpublic Iterator<K> iterator()
public int lastIndexOf(Object key)
key
- the key to find the index ofpublic List<K> sequence()
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()
keySet()
public Object remove(int index)
index
- The index of the object to remove.key
, or
null
if none existed.ArrayIndexOutOfBoundsException
- if the index
is
< 0
or >
the size of the
map.public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
readExternal
in interface Externalizable
in
- the stream to deserialize fromIOException
- if the stream raises itClassNotFoundException
- if the stream raises itpublic void writeExternal(ObjectOutput out) throws IOException
writeExternal
in interface Externalizable
out
- the stream to serialize toIOException
- if the stream raises itpublic V touch(Object key)
key
- See Map.get(Object)
get(Object)
does) but also updates the position of the associated
entry so that it becomes the most recently added in the list.get(Object)
Copyright © 2024 levigo holding gmbh. All rights reserved.