Class LinkedMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.apache.commons.collections4.map.AbstractHashedMap<K,V>
-
- org.apache.commons.collections4.map.AbstractLinkedMap<K,V>
-
- org.apache.commons.collections4.map.LinkedMap<K,V>
-
- Type Parameters:
K
- the type of the keys in this mapV
- the type of the values in this map
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
,Get<K,V>
,IterableGet<K,V>
,IterableMap<K,V>
,OrderedMap<K,V>
,Put<K,V>
public class LinkedMap<K,V> extends AbstractLinkedMap<K,V> implements java.io.Serializable, java.lang.Cloneable
AMap
implementation that maintains the order of the entries. In this implementation order is maintained by original insertion.This implementation improves on the JDK1.4 LinkedHashMap by adding the
MapIterator
functionality, additional convenience methods and allowing bidirectional iteration. It also implementsOrderedMap
. In addition, non-interface methods are provided to access the map by index.The
orderedMapIterator()
method provides direct access to a bidirectional iterator. The iterators from the other views can also be cast toOrderedIterator
if required.All the available iterators can be reset back to the start by casting to
ResettableIterator
and callingreset()
.The implementation is also designed to be subclassed, with lots of useful methods exposed.
Note that LinkedMap 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.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
LinkedMap.LinkedMapList<K>
List view of map.-
Nested classes/interfaces inherited from class org.apache.commons.collections4.map.AbstractLinkedMap
AbstractLinkedMap.EntrySetIterator<K,V>, AbstractLinkedMap.KeySetIterator<K>, AbstractLinkedMap.LinkEntry<K,V>, AbstractLinkedMap.LinkIterator<K,V>, AbstractLinkedMap.LinkMapIterator<K,V>, AbstractLinkedMap.ValuesIterator<V>
-
Nested classes/interfaces inherited from class org.apache.commons.collections4.map.AbstractHashedMap
AbstractHashedMap.EntrySet<K,V>, AbstractHashedMap.HashEntry<K,V>, AbstractHashedMap.HashIterator<K,V>, AbstractHashedMap.HashMapIterator<K,V>, AbstractHashedMap.KeySet<K>, AbstractHashedMap.Values<V>
-
-
Field Summary
Fields Modifier and Type Field Description private static long
serialVersionUID
Serialisation version-
Fields inherited from class org.apache.commons.collections4.map.AbstractLinkedMap
header
-
Fields inherited from class org.apache.commons.collections4.map.AbstractHashedMap
data, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD, entrySet, GETKEY_INVALID, GETVALUE_INVALID, keySet, loadFactor, MAXIMUM_CAPACITY, modCount, NO_NEXT_ENTRY, NO_PREVIOUS_ENTRY, NULL, REMOVE_INVALID, SETVALUE_INVALID, size, threshold, values
-
-
Constructor Summary
Constructors Constructor Description LinkedMap()
Constructs a new empty map with default size and load factor.LinkedMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity.LinkedMap(int initialCapacity, float loadFactor)
Constructs a new, empty map with the specified initial capacity and load factor.LinkedMap(java.util.Map<? extends K,? extends V> map)
Constructor copying elements from another map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<K>
asList()
Gets an unmodifiable List view of the keys.LinkedMap<K,V>
clone()
Clones the map without cloning the keys or values.K
get(int index)
Gets the key at the specified index.V
getValue(int index)
Gets the value at the specified index.int
indexOf(java.lang.Object key)
Gets the index of the specified key.private void
readObject(java.io.ObjectInputStream in)
Read the map in using a custom routine.V
remove(int index)
Removes the element at the specified index.private void
writeObject(java.io.ObjectOutputStream out)
Write the map out using a custom routine.-
Methods inherited from class org.apache.commons.collections4.map.AbstractLinkedMap
addEntry, clear, containsValue, createEntry, createEntrySetIterator, createKeySetIterator, createValuesIterator, entryAfter, entryBefore, firstKey, getEntry, getEntry, init, lastKey, mapIterator, nextKey, previousKey, removeEntry
-
Methods inherited from class org.apache.commons.collections4.map.AbstractHashedMap
addMapping, calculateNewCapacity, calculateThreshold, checkCapacity, containsKey, convertKey, destroyEntry, doReadObject, doWriteObject, ensureCapacity, entryHashCode, entryKey, entryNext, entrySet, entryValue, equals, get, hash, hashCode, hashIndex, isEmpty, isEqualKey, isEqualValue, keySet, put, putAll, remove, removeMapping, reuseEntry, size, toString, updateEntry, values
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.collections4.Get
containsKey, entrySet, get, isEmpty, keySet, remove, size, values
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialisation version- See Also:
- Constant Field Values
-
-
Constructor Detail
-
LinkedMap
public LinkedMap()
Constructs a new empty map with default size and load factor.
-
LinkedMap
public LinkedMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity- Throws:
java.lang.IllegalArgumentException
- if the initial capacity is negative
-
LinkedMap
public LinkedMap(int initialCapacity, float loadFactor)
Constructs a new, empty map with the specified initial capacity and load factor.- Parameters:
initialCapacity
- the initial capacityloadFactor
- the load factor- Throws:
java.lang.IllegalArgumentException
- if the initial capacity is negativejava.lang.IllegalArgumentException
- if the load factor is less than zero
-
-
Method Detail
-
clone
public LinkedMap<K,V> clone()
Clones the map without cloning the keys or values.- Overrides:
clone
in classAbstractHashedMap<K,V>
- Returns:
- a shallow clone
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
Write the map out using a custom routine.- Parameters:
out
- the output stream- Throws:
java.io.IOException
- if an error occurs while writing to the stream
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
Read the map in using a custom routine.- Parameters:
in
- the input stream- Throws:
java.io.IOException
- if an error occurs while reading from the streamjava.lang.ClassNotFoundException
- if an object read from the stream can not be loaded
-
get
public K get(int index)
Gets the key at the specified index.- Parameters:
index
- the index to retrieve- Returns:
- the key at the specified index
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is invalid
-
getValue
public V getValue(int index)
Gets the value at the specified index.- Parameters:
index
- the index to retrieve- Returns:
- the value at the specified index
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is invalid
-
indexOf
public int indexOf(java.lang.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
-
remove
public V 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
, ornull
if none existed - Throws:
java.lang.IndexOutOfBoundsException
- if the index is invalid
-
asList
public java.util.List<K> asList()
Gets an unmodifiable List view of the keys.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
AbstractHashedMap.keySet()
.- Returns:
- The ordered list of keys.
- See Also:
AbstractHashedMap.keySet()
-
-