|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.map.AbstractMapDecorator<K,V> org.apache.commons.collections.map.ListOrderedMap<K,V>
public class ListOrderedMap<K,V>
Decorates a Map
to ensure that the order of addition is retained
using a List
to maintain order.
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.
This class is Serializable from Commons Collections 3.1.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Field Summary | |
---|---|
protected List<K> |
insertOrder
Internal list to hold the sequence of objects |
Fields inherited from class org.apache.commons.collections.map.AbstractMapDecorator |
---|
map |
Constructor Summary | |
---|---|
|
ListOrderedMap()
Constructs a new empty ListOrderedMap that decorates
a HashMap . |
protected |
ListOrderedMap(Map<K,V> map)
Constructor that wraps (not copies). |
Method Summary | ||
---|---|---|
List |
asList()
Gets an unmodifiable List view of the keys which changes as the map changes. |
|
void |
clear()
|
|
static
|
decorate(Map<K,V> map)
Factory method to create an ordered map. |
|
Set |
entrySet()
|
|
K |
firstKey()
Gets the first key in this map by insert order. |
|
Object |
get(int index)
Gets the key at the specified index. |
|
Object |
getValue(int index)
Gets the value at the specified index. |
|
int |
indexOf(Object key)
Gets the index of the specified key. |
|
Set<K> |
keySet()
|
|
K |
lastKey()
Gets the last key in this map by insert order. |
|
MapIterator<K,V> |
mapIterator()
Obtains a MapIterator over the map. |
|
K |
nextKey(K key)
Gets the next key to the one specified using insert order. |
|
OrderedMapIterator<K,V> |
orderedMapIterator()
Obtains an OrderedMapIterator over the map. |
|
K |
previousKey(K key)
Gets the previous key to the one specified using insert order. |
|
V |
put(K key,
V value)
|
|
void |
putAll(Map<? extends K,? extends V> map)
|
|
Object |
remove(int index)
Removes the element at the specified index. |
|
V |
remove(Object key)
|
|
String |
toString()
Returns the Map as a string. |
|
Collection |
values()
|
Methods inherited from class org.apache.commons.collections.map.AbstractMapDecorator |
---|
containsKey, containsValue, equals, get, getMap, hashCode, isEmpty, size |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
---|
containsKey, containsValue, equals, get, hashCode, isEmpty, size |
Field Detail |
---|
protected final List<K> insertOrder
Constructor Detail |
---|
public ListOrderedMap()
ListOrderedMap
that decorates
a HashMap
.
protected ListOrderedMap(Map<K,V> map)
map
- the map to decorate, must not be null
IllegalArgumentException
- if map is nullMethod Detail |
---|
public static <K,V> OrderedMap<K,V> decorate(Map<K,V> map)
ArrayList
is used to retain order.
map
- the map to decorate, must not be null
IllegalArgumentException
- if map is nullpublic MapIterator<K,V> 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<K,V>
public OrderedMapIterator<K,V> 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<K,V>
public K firstKey()
firstKey
in interface OrderedMap<K,V>
NoSuchElementException
- if this map is emptypublic K lastKey()
lastKey
in interface OrderedMap<K,V>
NoSuchElementException
- if this map is emptypublic K nextKey(K key)
nextKey
in interface OrderedMap<K,V>
key
- the key to find previous for
public K previousKey(K key)
previousKey
in interface OrderedMap<K,V>
key
- the key to find previous for
public V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMapDecorator<K,V>
public void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<K,V>
putAll
in class AbstractMapDecorator<K,V>
public V remove(Object key)
remove
in interface Map<K,V>
remove
in class AbstractMapDecorator<K,V>
public void clear()
clear
in interface Map<K,V>
clear
in class AbstractMapDecorator<K,V>
public Set<K> keySet()
keySet
in interface Map<K,V>
keySet
in class AbstractMapDecorator<K,V>
public Collection values()
values
in interface Map<K,V>
values
in class AbstractMapDecorator<K,V>
public Set entrySet()
entrySet
in interface Map<K,V>
entrySet
in class AbstractMapDecorator<K,V>
public String toString()
toString
in class AbstractMapDecorator<K,V>
public Object get(int index)
index
- the index to retrieve
IndexOutOfBoundsException
- if the index is invalidpublic Object getValue(int index)
index
- the index to retrieve
IndexOutOfBoundsException
- if the index is invalidpublic int indexOf(Object key)
key
- the key to find the index of
public Object remove(int index)
index
- the index of the object to remove
key
,
or null
if none existed
IndexOutOfBoundsException
- if the index is invalidpublic List asList()
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()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |