org.apache.commons.collections.map
Class ListOrderedMap
- IterableMap, Map, OrderedMap, Serializable
public class 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
java.util.Collections.synchronizedMap(Map)
. This class may throw
exceptions when accessed by concurrent threads without synchronization.
This class is Serializable from Commons Collections 3.1.
$Revision: 365406 $ $Date: 2006-01-02 19:34:53 +0000 (Mon, 02 Jan 2006) $- Henri Yandell
- Stephen Colebourne
- Matt Benson
protected List | insertOrder - Internal list to hold the sequence of objects
|
ListOrderedMap() - Constructs a new empty
ListOrderedMap that decorates
a HashMap .
|
ListOrderedMap(Map map) - Constructor that wraps (not copies).
|
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() - Gets a view over the entries in the map.
|
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.
|
List | keyList() - Gets a view over the keys in the map as a List.
|
Set | keySet() - Gets a view over the keys in the map.
|
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)
|
Object | put(int index, Object key, Object value) - Puts a key-value mapping into the map at the specified index.
|
void | putAll(Map map)
|
Object | remove(Object key)
|
Object | remove(int index) - Removes the element at the specified index.
|
Object | setValue(int index, Object value) - Sets the value at the specified index.
|
String | toString() - Returns the Map as a string.
|
List | valueList() - Gets a view over the values in the map as a List.
|
Collection | values() - Gets a view over the values in the map.
|
clear , containsKey , containsValue , entrySet , equals , get , getMap , hashCode , isEmpty , keySet , put , putAll , remove , size , toString , values |
insertOrder
protected final List insertOrder
Internal list to hold the sequence of objects
ListOrderedMap
public ListOrderedMap()
Constructs a new empty ListOrderedMap
that decorates
a HashMap
.
ListOrderedMap
protected ListOrderedMap(Map map)
Constructor that wraps (not copies).
map
- the map to decorate, must not be null
asList
public List asList()
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
java.util.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()
.
- The ordered list of keys.
decorate
public static OrderedMap decorate(Map map)
Factory method to create an ordered map.
An
ArrayList
is used to retain order.
map
- the map to decorate, must not be null
entrySet
public Set entrySet()
Gets a view over the entries in the map.
The Set will be ordered by object insertion into the map.
- entrySet in interface AbstractMapDecorator
- the fully modifiable set view over the entries
firstKey
public Object firstKey()
Gets the first key in this map by insert order.
- firstKey in interface OrderedMap
- the first key currently in this map
get
public Object get(int index)
Gets the key at the specified index.
index
- the index to retrieve
- the key at the specified index
getValue
public Object getValue(int index)
Gets the value at the specified index.
index
- the index to retrieve
- the key at the specified index
indexOf
public int indexOf(Object key)
Gets the index of the specified key.
key
- the key to find the index of
- the index, or -1 if not found
keyList
public List keyList()
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.
- the unmodifiable list view over the keys
keySet
public Set keySet()
Gets a view over the keys in the map.
The Collection will be ordered by object insertion into the map.
- keySet in interface AbstractMapDecorator
- the fully modifiable collection view over the keys
lastKey
public Object lastKey()
Gets the last key in this map by insert order.
- lastKey in interface OrderedMap
- the last key currently in this map
mapIterator
public MapIterator mapIterator()
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");
}
- mapIterator in interface IterableMap
nextKey
public Object nextKey(Object key)
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).
- nextKey in interface OrderedMap
key
- the key to find previous for
- the next key, null if no match or at start
orderedMapIterator
public OrderedMapIterator orderedMapIterator()
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();
}
- orderedMapIterator in interface OrderedMap
previousKey
public Object previousKey(Object key)
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).
- previousKey in interface OrderedMap
key
- the key to find previous for
- the previous key, null if no match or at start
put
public Object put(int index,
Object key,
Object value)
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 insertedkey
- the keyvalue
- the value
- the value previously mapped to the key
remove
public Object remove(int index)
Removes the element at the specified index.
index
- the index of the object to remove
- the removed value, or
null
if none existed
setValue
public Object setValue(int index,
Object value)
Sets the value at the specified index.
index
- the index of the value to set
- the previous value at that index
valueList
public List valueList()
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.
- the partially modifiable list view over the values
values
public Collection values()
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()
- values in interface AbstractMapDecorator
- the fully modifiable collection view over the values
Copyright © 2001-2008 Apache Software Foundation. All Rights Reserved.