java.lang.Object | ||
↳ | org.apache.commons.collections.map.AbstractMapDecorator | |
↳ | org.apache.commons.collections.map.ListOrderedMap |
Decorates a 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 synchronizedMap(Map)
. This class may throw
exceptions when accessed by concurrent threads without synchronization.
This class is Serializable from Commons Collections 3.1.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
insertOrder | Internal list to hold the sequence of objects |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.apache.commons.collections.map.AbstractMapDecorator
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new empty
ListOrderedMap that decorates
a HashMap . |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructor that wraps (not copies).
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Gets an unmodifiable List view of the keys which changes as the map changes.
| |||||||||||
Factory method to create an ordered map.
| |||||||||||
Gets a view over the entries in the map.
| |||||||||||
Gets the first key in this map by insert order.
| |||||||||||
Gets the key at the specified index.
| |||||||||||
Gets the value at the specified index.
| |||||||||||
Gets the index of the specified key.
| |||||||||||
Gets a view over the keys in the map as a List.
| |||||||||||
Gets a view over the keys in the map.
| |||||||||||
Gets the last key in this map by insert order.
| |||||||||||
Obtains a
MapIterator over the map. | |||||||||||
Gets the next key to the one specified using insert order.
| |||||||||||
Obtains an
OrderedMapIterator over the map. | |||||||||||
Gets the previous key to the one specified using insert order.
| |||||||||||
Puts a key-value mapping into the map at the specified index.
| |||||||||||
Removes the element at the specified index.
| |||||||||||
Sets the value at the specified index.
| |||||||||||
Returns the Map as a string.
| |||||||||||
Gets a view over the values in the map as a List.
| |||||||||||
Gets a view over the values in the map.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.apache.commons.collections.map.AbstractMapDecorator
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.util.Map
| |||||||||||
From interface
org.apache.commons.collections.IterableMap
| |||||||||||
From interface
org.apache.commons.collections.OrderedMap
|
Constructs a new empty ListOrderedMap
that decorates
a HashMap
.
Constructor that wraps (not copies).
map | the map to decorate, must not be null |
---|
IllegalArgumentException | if map is null |
---|
Gets an unmodifiable List view of the keys which changes as the map changes.
The returned list is unmodifiable 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 the better named
keyList()
or keySet()
.
Factory method to create an ordered map.
An ArrayList
is used to retain order.
map | the map to decorate, must not be null |
---|
IllegalArgumentException | if map is null |
---|
Gets a view over the entries in the map.
The Set will be ordered by object insertion into the map.
Gets the first key in this map by insert order.
NoSuchElementException | if this map is empty |
---|
Gets the key at the specified index.
index | the index to retrieve |
---|
IndexOutOfBoundsException | if the index is invalid |
---|
Gets the value at the specified index.
index | the index to retrieve |
---|
IndexOutOfBoundsException | if the index is invalid |
---|
Gets the index of the specified key.
key | the key to find the index of |
---|
Gets a view over the keys in the map as a List.
The List will be ordered by object insertion into the map. The List is unmodifiable.
Gets a view over the keys in the map.
The Collection will be ordered by object insertion into the map.
Gets the last key in this map by insert order.
NoSuchElementException | if this map is empty |
---|
Obtains a 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 cast to Map Entry objects.
IterableMap map = new HashedMap(); MapIterator it = map.mapIterator(); while (it.hasNext()) { Object key = it.next(); Object value = it.getValue(); it.setValue("newValue"); }
Gets the next key to the one specified using insert order. This method performs a list search to find the key and is O(n).
key | the key to find previous for |
---|
Obtains an OrderedMapIterator
over the map.
A ordered map iterator is an efficient way of iterating over maps in both directions.
BidiMap map = new TreeBidiMap(); MapIterator it = map.mapIterator(); while (it.hasNext()) { Object key = it.next(); Object value = it.getValue(); it.setValue("newValue"); Object previousKey = it.previous(); }
Gets the previous key to the one specified using insert order. This method performs a list search to find the key and is O(n).
key | the key to find previous for |
---|
Puts a key-value mapping into the map at the specified index.
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 ocurred.
index | the index at which the mapping should be inserted |
---|---|
key | the key |
value | the value |
IndexOutOfBoundsException | if the index is out of range |
---|
Removes the element at the specified index.
index | the index of the object to remove |
---|
null
if none existedIndexOutOfBoundsException | if the index is invalid |
---|
Sets the value at the specified index.
index | the index of the value to set |
---|
IndexOutOfBoundsException | if the index is invalid |
---|
Gets a view over the values in the map as a List.
The List will be ordered by object insertion into the map. The List supports remove and set, but does not support add.
Gets a view over the values in the map.
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()