Map 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 implements OrderedMap.
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
to OrderedIterator if required.
All the available iterators can be reset back to the start by casting to
ResettableIterator and calling reset().
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 java.. This class may throw
exceptions when accessed by concurrent threads without synchronization.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Serialisation version |
| Access | Constructor and Description |
|---|---|
| public | |
| public | LinkedMap(final int
the initial capacity initialCapacity)Constructs a new, empty map with the specified initial capacity. |
| public | LinkedMap(final int
the initial capacity initialCapacity, final float the load factor loadFactor)Constructs a new, empty map with the specified initial capacity and load factor. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public List | |
| public LinkedMap | Returns: a shallow cloneOverrides org. |
| public K | Returns: the key at the specified indexthe index to retrieve index)Gets the key at the specified index. |
| public V | Returns: the value at the specified indexthe index to retrieve index)Gets the value at the specified index. |
| public int | |
| private void | |
| public V | Returns: the previous value corresponding thekey,
or null if none existedthe index of the object to remove index)Removes the element at the specified index. |
| private void |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialisation version | |
| LinkedMap | back to summary |
|---|---|
| public LinkedMap() Constructs a new empty map with default size and load factor. | |
| LinkedMap | back to summary |
|---|---|
| public LinkedMap(final int initialCapacity) Constructs a new, empty map with the specified initial capacity.
| |
| LinkedMap | back to summary |
|---|---|
| public LinkedMap(final int initialCapacity, final float loadFactor) Constructs a new, empty map with the specified initial capacity and load factor.
| |
| LinkedMap | back to summary |
|---|---|
| public LinkedMap(final Map<? extends K, ? extends V> map) Constructor copying elements from another map.
| |
| asList | back to summary |
|---|---|
| public List Gets an unmodifiable List view of the keys.
The returned list is unmodifiable because changes to the values of
the list (using
An alternative to this method is to use | |
| clone | back to summary |
|---|---|
| public LinkedMap Overrides org. Clones the map without cloning the keys or values. | |
| get | back to summary |
|---|---|
| public K get(final int index) Gets the key at the specified index.
| |
| getValue | back to summary |
|---|---|
| public V getValue(final int index) Gets the value at the specified index.
| |
| indexOf | back to summary |
|---|---|
| public int indexOf(Object key) Gets the index of the specified key.
| |
| readObject | back to summary |
|---|---|
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException Read the map in using a custom routine.
| |
| remove | back to summary |
|---|---|
| public V remove(final int index) Removes the element at the specified index.
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(final ObjectOutputStream out) throws IOException Write the map out using a custom routine.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final LinkedMap |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Overrides java. Implements java. |
| public boolean | contains(final Object
element whose presence in this list is to be tested obj)Overrides java. Implements java. true if this list contains the specified element.
|
| public boolean | containsAll(final Collection<?>
collection to be checked for containment in this list coll)Overrides java. Implements java. true if this list contains all of the elements of the
specified collection.
|
| public K | get(final int
index of the element to return index)Implements abstract java. Implements java. |
| public int | indexOf(final Object
element to search for obj)Overrides java. Implements java. |
| public Iterator | iterator()
Overrides java. Implements java. |
| public int | lastIndexOf(final Object
element to search for obj)Overrides java. Implements java. |
| public ListIterator | listIterator()
Overrides java. Implements java. |
| public ListIterator | listIterator(final int
index of the first element to be returned from the
list iterator (by a call to fromIndex)next)Overrides java. Implements java. |
| public K | remove(final int
the index of the element to be removed index)Overrides java. Implements java. |
| public boolean | remove(final Object
element to be removed from this list, if present obj)Overrides java. Implements java. |
| public boolean | removeAll(final Collection<?>
collection containing elements to be removed from this list coll)Overrides java. Implements java. |
| public boolean | removeIf(final Predicate<? super K>
a predicate which returns filter)true for elements to be
removedOverrides default java. |
| public boolean | retainAll(final Collection<?>
collection containing elements to be retained in this list coll)Overrides java. Implements java. |
| public int | size()
Implements abstract java. Implements java. |
| public List | subList(final int
low endpoint (inclusive) of the subList fromIndexInclusive, final int high endpoint (exclusive) of the subList toIndexExclusive)Overrides java. Implements java. fromIndex, inclusive, and toIndex, exclusive.
|
| public Object[] | toArray()
Overrides java. Implements java. |
| public <T> T[] | toArray(final T[]
the array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose. array)Overrides java. Implements java. |
| parent | back to summary |
|---|---|
| private final LinkedMap<K, ?> parent | |
| LinkedMapList | back to summary |
|---|---|
| pack-priv LinkedMapList(final LinkedMap<K, ?> parent) | |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Removes all of the elements from this list (optional operation). The list will be empty after this call returns.
| |
| contains | back to summary |
|---|---|
| public boolean contains(final Object obj) Overrides java. Implements java. Doc from java. Returns | |
| containsAll | back to summary |
|---|---|
| public boolean containsAll(final Collection<?> coll) Overrides java. Implements java. Doc from java. Returns
| |
| get | back to summary |
|---|---|
| public K get(final int index) Implements abstract java. Implements java. Doc from java. Returns the element at the specified position in this list.
| |
| indexOf | back to summary |
|---|---|
| public int indexOf(final Object obj) Overrides java. Implements java. Doc from java. Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the lowest index
| |
| iterator | back to summary |
|---|---|
| public Iterator Overrides java. Implements java. Doc from java. Returns an iterator over the elements in this list in proper sequence. | |
| lastIndexOf | back to summary |
|---|---|
| public int lastIndexOf(final Object obj) Overrides java. Implements java. Doc from java. Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
More formally, returns the highest index
| |
| listIterator | back to summary |
|---|---|
| public ListIterator Overrides java. Implements java. Doc from java. Returns a list iterator over the elements in this list (in proper sequence).
| |
| listIterator | back to summary |
|---|---|
| public ListIterator Overrides java. Implements java. Doc from java. Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
The specified index indicates the first element that would be
returned by an initial call to
| |
| remove | back to summary |
|---|---|
| public K remove(final int index) Overrides java. Implements java. Doc from java. Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
| |
| remove | back to summary |
|---|---|
| public boolean remove(final Object obj) Overrides java. Implements java. Doc from java. Removes the first occurrence of the specified element from this list,
if it is present (optional operation). If this list does not contain
the element, it is unchanged. More formally, removes the element with
the lowest index | |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Collection<?> coll) Overrides java. Implements java. Doc from java. Removes from this list all of its elements that are contained in the specified collection (optional operation).
| |
| removeIf | back to summary |
|---|---|
| public boolean removeIf(final Predicate<? super K> filter) Overrides default java. Doc from java. Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller. | |
| retainAll | back to summary |
|---|---|
| public boolean retainAll(final Collection<?> coll) Overrides java. Implements java. Doc from java. Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.
| |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this list. If this list contains
more than
| |
| subList | back to summary |
|---|---|
| public List Overrides java. Implements java. Doc from java. Returns a view of the portion of this list between the specified
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
| |
| toArray | back to summary |
|---|---|
| public Object[] toArray() Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this list in proper sequence (from first to last element). The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs. | |
| toArray | back to summary |
|---|---|
| public <T> T[] toArray(final T[] array) Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e.,
the array has more elements than the list), the element in the array
immediately following the end of the list is set to Like the Suppose
Note that toArray(new Object[0]) is identical in function to
toArray().
| |