|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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.
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.
Inner classes inherited from class java.util.Map |
Map.Entry |
Field Summary | |
protected List |
insertOrder
Internal list to hold the sequence of objects |
Fields inherited from class org.apache.commons.collections.map.AbstractMapDecorator |
map |
Constructor Summary | |
protected |
ListOrderedMap(Map 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 OrderedMap |
decorate(Map map)
Factory method to create an ordered map. |
Set |
entrySet()
|
Object |
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 |
keySet()
|
Object |
lastKey()
Gets the last key in this map by insert order. |
MapIterator |
mapIterator()
Obtains a MapIterator over the map. |
Object |
nextKey(Object key)
Gets the next key to the one specified using insert order. |
OrderedMapIterator |
orderedMapIterator()
Obtains an OrderedMapIterator over the map. |
Object |
previousKey(Object key)
Gets the previous key to the one specified using insert order. |
Object |
put(Object key,
Object value)
|
void |
putAll(Map map)
|
Object |
remove(int index)
Removes the element at the specified index. |
Object |
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 insertOrder
Constructor Detail |
protected ListOrderedMap(Map map)
map
- the map to decorate, must not be nullIllegalArgumentException
- if map is nullMethod Detail |
public static OrderedMap decorate(Map map)
An ArrayList
is used to retain order.
map
- the map to decorate, must not be nullIllegalArgumentException
- 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
org.apache.commons.collections.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
org.apache.commons.collections.OrderedMap
public Object firstKey()
firstKey
in interface OrderedMap
NoSuchElementException
- if this map is emptypublic Object lastKey()
lastKey
in interface OrderedMap
NoSuchElementException
- if this map is emptypublic Object nextKey(Object key)
nextKey
in interface OrderedMap
key
- the key to find previous forpublic Object previousKey(Object key)
previousKey
in interface OrderedMap
key
- the key to find previous forpublic Object put(Object key, Object value)
put
in interface Map
put
in class AbstractMapDecorator
public void putAll(Map map)
putAll
in interface Map
putAll
in class AbstractMapDecorator
public Object remove(Object key)
remove
in interface Map
remove
in class AbstractMapDecorator
public void clear()
clear
in interface Map
clear
in class AbstractMapDecorator
public Set keySet()
keySet
in interface Map
keySet
in class AbstractMapDecorator
public Collection values()
values
in interface Map
values
in class AbstractMapDecorator
public Set entrySet()
entrySet
in interface Map
entrySet
in class AbstractMapDecorator
public String toString()
toString
in class AbstractMapDecorator
public Object get(int index)
index
- the index to retrieveIndexOutOfBoundsException
- if the index is invalidpublic Object getValue(int index)
index
- the index to retrieveIndexOutOfBoundsException
- if the index is invalidpublic int indexOf(Object key)
key
- the key to find the index ofpublic Object remove(int index)
index
- the index of the object to removekey
,
or null
if none existedIndexOutOfBoundsException
- if the index is invalidpublic 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 keySet()
.
keySet()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |