public class ListOrderedMap extends AbstractMapDecorator implements OrderedMap, 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.
This class is Serializable from Commons Collections 3.1.
Constructor and Description |
---|
ListOrderedMap()
Constructs a new empty
ListOrderedMap that decorates
a HashMap . |
Modifier and Type | Method and Description |
---|---|
java.util.List |
asList()
Gets an unmodifiable List view of the keys which changes as the map changes.
|
void |
clear() |
static OrderedMap |
decorate(java.util.Map map)
Factory method to create an ordered map.
|
java.util.Set |
entrySet()
Gets a view over the entries in the map.
|
java.lang.Object |
firstKey()
Gets the first key in this map by insert order.
|
java.lang.Object |
get(int index)
Gets the key at the specified index.
|
java.lang.Object |
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 |
keyList()
Gets a view over the keys in the map as a List.
|
java.util.Set |
keySet()
Gets a view over the keys in the map.
|
java.lang.Object |
lastKey()
Gets the last key in this map by insert order.
|
MapIterator |
mapIterator()
Obtains a
MapIterator over the map. |
java.lang.Object |
nextKey(java.lang.Object key)
Gets the next key to the one specified using insert order.
|
OrderedMapIterator |
orderedMapIterator()
Obtains an
OrderedMapIterator over the map. |
java.lang.Object |
previousKey(java.lang.Object key)
Gets the previous key to the one specified using insert order.
|
java.lang.Object |
put(int index,
java.lang.Object key,
java.lang.Object value)
Puts a key-value mapping into the map at the specified index.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value) |
void |
putAll(java.util.Map map) |
java.lang.Object |
remove(int index)
Removes the element at the specified index.
|
java.lang.Object |
remove(java.lang.Object key) |
java.lang.Object |
setValue(int index,
java.lang.Object value)
Sets the value at the specified index.
|
java.lang.String |
toString()
Returns the Map as a string.
|
java.util.List |
valueList()
Gets a view over the values in the map as a List.
|
java.util.Collection |
values()
Gets a view over the values in the map.
|
containsKey, containsValue, equals, get, hashCode, isEmpty, size
public ListOrderedMap()
ListOrderedMap
that decorates
a HashMap
.public static OrderedMap decorate(java.util.Map map)
An ArrayList
is used to retain order.
map
- the map to decorate, must not be nulljava.lang.IllegalArgumentException
- if map is nullpublic MapIterator mapIterator()
IterableMap
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"); }
mapIterator
in interface IterableMap
public OrderedMapIterator orderedMapIterator()
OrderedMap
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(); }
orderedMapIterator
in interface OrderedMap
public java.lang.Object firstKey()
firstKey
in interface OrderedMap
java.util.NoSuchElementException
- if this map is emptypublic java.lang.Object lastKey()
lastKey
in interface OrderedMap
java.util.NoSuchElementException
- if this map is emptypublic java.lang.Object nextKey(java.lang.Object key)
nextKey
in interface OrderedMap
key
- the key to find previous forpublic java.lang.Object previousKey(java.lang.Object key)
previousKey
in interface OrderedMap
key
- the key to find previous forpublic java.lang.Object put(java.lang.Object key, java.lang.Object value)
put
in interface java.util.Map
put
in class AbstractMapDecorator
public void putAll(java.util.Map map)
putAll
in interface java.util.Map
putAll
in class AbstractMapDecorator
public java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
remove
in class AbstractMapDecorator
public void clear()
clear
in interface java.util.Map
clear
in class AbstractMapDecorator
public java.util.Set keySet()
The Collection will be ordered by object insertion into the map.
keySet
in interface java.util.Map
keySet
in class AbstractMapDecorator
keyList()
public java.util.List keyList()
The List will be ordered by object insertion into the map. The List is unmodifiable.
keySet()
public java.util.Collection 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()
values
in interface java.util.Map
values
in class AbstractMapDecorator
valueList()
public java.util.List 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 entrySet()
The Set will be ordered by object insertion into the map.
entrySet
in interface java.util.Map
entrySet
in class AbstractMapDecorator
public java.lang.String toString()
toString
in class AbstractMapDecorator
public java.lang.Object get(int index)
index
- the index to retrievejava.lang.IndexOutOfBoundsException
- if the index is invalidpublic java.lang.Object 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 java.lang.Object setValue(int index, java.lang.Object value)
index
- the index of the value to setjava.lang.IndexOutOfBoundsException
- if the index is invalidpublic java.lang.Object put(int index, java.lang.Object key, java.lang.Object 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 ocurred.
index
- the index at which the mapping should be insertedkey
- the keyvalue
- the valuejava.lang.IndexOutOfBoundsException
- if the index is out of rangepublic java.lang.Object remove(int index)
index
- the index of the object to removenull
if none existedjava.lang.IndexOutOfBoundsException
- if the index is invalidpublic java.util.List 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()
.
Copyright © 2010 - 2020 Adobe. All Rights Reserved