⇒ Index (Frames) |  ⇒ Index (No Frames) |  ⇒ Package |  ⇒ Package Tree |  ⇒ Full Tree 
org.apache.commons.collections.map

Class ListOrderedMap

java.lang.Object
|
+--org.apache.commons.collections.map.AbstractMapDecorator
   |
   +--org.apache.commons.collections.map.ListOrderedMap

All Implemented Interfaces:
Map, OrderedMap


public class ListOrderedMap
extends AbstractMapDecorator
implements OrderedMap

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.

Since:
Commons Collections 3.0
Authors:
Henri Yandell
Stephen Colebourne

Field Summary

List

insertOrder

Internal list to hold the sequence of objects

Constructor Summary

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()

Object

nextKey(Object key)

Gets the next key to the one specified using insert order.

OrderedMapIterator

orderedMapIterator()

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(Object key)

Object

remove(int index)

Removes the element at the specified index.

String

toString()

Returns the Map as a string.

Collection

values()

Field Details

insertOrder

protected final List insertOrder

Internal list to hold the sequence of objects

Constructor Details

ListOrderedMap

protected ListOrderedMap(Map map)

Constructor that wraps (not copies).

Parameters:
map - the map to decorate, must not be null
Throws:
- if map is null

Method Details

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 keySet().

Returns:
The ordered list of keys.
See Also:
keySet()

clear

public void clear()


decorate

public static OrderedMap decorate(Map map)

Factory method to create an ordered map.

An ArrayList is used to retain order.

Parameters:
map - the map to decorate, must not be null
Throws:
- if map is null

entrySet

public Set entrySet()


firstKey

public Object firstKey()

Gets the first key in this map by insert order.

Returns:
the first key currently in this map
Throws:
- if this map is empty

get

public Object get(int index)

Gets the key at the specified index.

Parameters:
index - the index to retrieve
Returns:
the key at the specified index
Throws:
- if the index is invalid

getValue

public Object getValue(int index)

Gets the value at the specified index.

Parameters:
index - the index to retrieve
Returns:
the key at the specified index
Throws:
- if the index is invalid

indexOf

public int indexOf(Object key)

Gets the index of the specified key.

Parameters:
key - the key to find the index of
Returns:
the index, or -1 if not found

keySet

public Set keySet()


lastKey

public Object lastKey()

Gets the last key in this map by insert order.

Returns:
the last key currently in this map
Throws:
- if this map is empty

mapIterator

public MapIterator mapIterator()


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).

Parameters:
key - the key to find previous for
Returns:
the next key, null if no match or at start

orderedMapIterator

public OrderedMapIterator orderedMapIterator()


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).

Parameters:
key - the key to find previous for
Returns:
the previous key, null if no match or at start

put

public Object put(Object key, Object value)

Parameters:
key
value

putAll

public void putAll(Map map)

Parameters:
map

remove

public Object remove(int index)

Removes the element at the specified index.

Parameters:
index - the index of the object to remove
Returns:
the previous value corresponding the key, or null if none existed
Throws:
- if the index is invalid

remove

public Object remove(Object key)

Parameters:
key

toString

public String toString()

Returns the Map as a string.

Returns:
the Map as a String

values

public Collection values()