K
- the type of the keys in this mapV
- the type of the values in this mappublic class ListOrderedMap<K,V> extends AbstractMapDecorator<K,V> implements OrderedMap<K,V>, java.io.Serializable
Map
to ensure that the order of addition is retained
using a List
to maintain order.
The order will be used via the iterators and toArray methods on the views.
The order is also returned by the MapIterator
.
The orderedMapIterator()
method accesses an iterator that can
iterate both forwards and backwards through the map.
In addition, non-interface methods are provided to access the map by index.
If an object is added to the Map for a second time, it will remain in the original position in the iteration.
Note that ListOrderedMap is not synchronized and is not thread-safe.
If you wish to use this map from multiple threads concurrently, you must use
appropriate synchronization. The simplest approach is to wrap this map
using Collections.synchronizedMap(Map)
. This class may throw
exceptions when accessed by concurrent threads without synchronization.
Note that ListOrderedMap doesn't work with
IdentityHashMap
, CaseInsensitiveMap
,
or similar maps that violate the general contract of Map
.
The ListOrderedMap
(or, more precisely, the underlying List
)
is relying on equals()
. This is fine, as long as the
decorated Map
is also based on equals()
,
and hashCode()
, which
IdentityHashMap
, and
CaseInsensitiveMap
don't: The former uses ==
, and
the latter uses equals()
on a lower-cased
key.
This class is Serializable
starting with Commons Collections 3.1.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
ListOrderedMap.EntrySetView<K,V> |
(package private) static class |
ListOrderedMap.KeySetView<K> |
(package private) static class |
ListOrderedMap.ListOrderedIterator<K,V> |
(package private) static class |
ListOrderedMap.ListOrderedMapEntry<K,V> |
(package private) static class |
ListOrderedMap.ListOrderedMapIterator<K,V> |
(package private) static class |
ListOrderedMap.ValuesView<V> |
Modifier and Type | Field and Description |
---|---|
private java.util.List<K> |
insertOrder
Internal list to hold the sequence of objects
|
private static long |
serialVersionUID
Serialization version
|
map
Modifier | Constructor and Description |
---|---|
|
ListOrderedMap()
Constructs a new empty
ListOrderedMap that decorates
a HashMap . |
protected |
ListOrderedMap(java.util.Map<K,V> map)
Constructor that wraps (not copies).
|
Modifier and Type | Method and Description |
---|---|
java.util.List<K> |
asList()
Gets an unmodifiable List view of the keys which changes as the map changes.
|
void |
clear() |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Gets a view over the entries in the map.
|
K |
firstKey()
Gets the first key in this map by insert order.
|
K |
get(int index)
Gets the key at the specified index.
|
V |
getValue(int index)
Gets the value at the specified index.
|
int |
indexOf(java.lang.Object key)
Gets the index of the specified key.
|
java.util.List<K> |
keyList()
Gets a view over the keys in the map as a List.
|
java.util.Set<K> |
keySet()
Gets a view over the keys in the map.
|
K |
lastKey()
Gets the last key in this map by insert order.
|
static <K,V> ListOrderedMap<K,V> |
listOrderedMap(java.util.Map<K,V> map)
Factory method to create an ordered map.
|
OrderedMapIterator<K,V> |
mapIterator()
Obtains a
MapIterator over the map. |
K |
nextKey(java.lang.Object key)
Gets the next key to the one specified using insert order.
|
K |
previousKey(java.lang.Object key)
Gets the previous key to the one specified using insert order.
|
V |
put(int index,
K key,
V value)
Puts a key-value mapping into the map at the specified index.
|
V |
put(K key,
V value)
Note that the return type is Object, rather than V as in the Map interface.
|
void |
putAll(int index,
java.util.Map<? extends K,? extends V> map)
Puts the values contained in a supplied Map into the Map starting at
the specified index.
|
void |
putAll(java.util.Map<? extends K,? extends V> map) |
private void |
readObject(java.io.ObjectInputStream in)
Read the map in using a custom routine.
|
V |
remove(int index)
Removes the element at the specified index.
|
V |
remove(java.lang.Object key) |
V |
setValue(int index,
V value)
Sets the value at the specified index.
|
java.lang.String |
toString()
Returns the Map as a string.
|
java.util.List<V> |
valueList()
Gets a view over the values in the map as a List.
|
java.util.Collection<V> |
values()
Gets a view over the values in the map.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Write the map out using a custom routine.
|
containsKey, containsValue, decorated, equals, get, hashCode, isEmpty, size
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove, replace, replace, replaceAll, size
containsKey, containsValue, get, isEmpty, size
private static final long serialVersionUID
private final java.util.List<K> insertOrder
public ListOrderedMap()
ListOrderedMap
that decorates
a HashMap
.public static <K,V> ListOrderedMap<K,V> listOrderedMap(java.util.Map<K,V> map)
An ArrayList
is used to retain order.
K
- the key typeV
- the value typemap
- the map to decorate, must not be nulljava.lang.NullPointerException
- if map is nullprivate void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output streamjava.io.IOException
- if an error occurs while writing to the streamprivate void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
in
- the input streamjava.io.IOException
- if an error occurs while reading from the streamjava.lang.ClassNotFoundException
- if an object read from the stream can not be loadedpublic OrderedMapIterator<K,V> mapIterator()
AbstractIterableMap
MapIterator
over the map.
A map iterator is an efficient way of iterating over maps. There is no need to access the entry set or use Map Entry objects.
IterableMap<String,Integer> map = new HashedMap<String,Integer>(); MapIterator<String,Integer> it = map.mapIterator(); while (it.hasNext()) { String key = it.next(); Integer value = it.getValue(); it.setValue(value + 1); }
mapIterator
in interface IterableGet<K,V>
mapIterator
in interface OrderedMap<K,V>
mapIterator
in class AbstractIterableMap<K,V>
public K firstKey()
firstKey
in interface OrderedMap<K,V>
java.util.NoSuchElementException
- if this map is emptypublic K lastKey()
lastKey
in interface OrderedMap<K,V>
java.util.NoSuchElementException
- if this map is emptypublic K nextKey(java.lang.Object key)
nextKey
in interface OrderedMap<K,V>
key
- the key to find previous forpublic K previousKey(java.lang.Object key)
previousKey
in interface OrderedMap<K,V>
key
- the key to find previous forpublic V put(K key, V value)
Put
put
in interface java.util.Map<K,V>
put
in interface Put<K,V>
put
in class AbstractMapDecorator<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keykey
, or
null
if there was no mapping for key
.
(A null
return can also indicate that the map
previously associated null
with key
,
if the implementation supports null
values.)Map.put(Object, Object)
public void putAll(int index, java.util.Map<? extends K,? extends V> map)
index
- the index in the Map to start at.map
- the Map containing the entries to be added.java.lang.IndexOutOfBoundsException
- if the index is out of range [0, size]public V remove(java.lang.Object key)
remove
in interface java.util.Map<K,V>
remove
in interface Get<K,V>
remove
in class AbstractMapDecorator<K,V>
key
- key whose mapping is to be removed from the mapkey
, or
null
if there was no mapping for key
.Map.remove(Object)
public void clear()
public java.util.Set<K> keySet()
The Collection will be ordered by object insertion into the map.
public java.util.List<K> keyList()
The List will be ordered by object insertion into the map. The List is unmodifiable.
keySet()
public java.util.Collection<V> values()
The Collection will be ordered by object insertion into the map.
From Commons Collections 3.2, this Collection can be cast
to a list, see valueList()
public java.util.List<V> valueList()
The List will be ordered by object insertion into the map. The List supports remove and set, but does not support add.
values()
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
The Set will be ordered by object insertion into the map.
public java.lang.String toString()
toString
in class AbstractMapDecorator<K,V>
public K get(int index)
index
- the index to retrievejava.lang.IndexOutOfBoundsException
- if the index is invalidpublic V getValue(int index)
index
- the index to retrievejava.lang.IndexOutOfBoundsException
- if the index is invalidpublic int indexOf(java.lang.Object key)
key
- the key to find the index ofpublic V setValue(int index, V value)
index
- the index of the value to setvalue
- the new value to setjava.lang.IndexOutOfBoundsException
- if the index is invalidpublic V put(int index, K key, V value)
If the map already contains the key, then the original mapping is removed and the new mapping added at the specified index. The remove may change the effect of the index. The index is always calculated relative to the original state of the map.
Thus the steps are: (1) remove the existing key-value mapping, then (2) insert the new key-value mapping at the position it would have been inserted had the remove not occurred.
index
- the index at which the mapping should be insertedkey
- the keyvalue
- the valuejava.lang.IndexOutOfBoundsException
- if the index is out of range [0, size]public V remove(int index)
index
- the index of the object to removenull
if none existedjava.lang.IndexOutOfBoundsException
- if the index is invalidpublic java.util.List<K> asList()
The returned list is unmodifiable because changes to the values of
the list (using 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 the better named
keyList()
or keySet()
.