Class IndexMap<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable
    Direct Known Subclasses:
    IndexMap.ListIndexMap, IndexMap.MapIndexMap

    public class IndexMap<K,​V>
    extends java.lang.Object
    implements java.io.Serializable, java.lang.Cloneable
    This class combines the list and map interface. So it provides fast lookup (via map) and ordering (via list). Because the remove(Object) method has different return types in map and list, index map cannot implement both interfaces. Methods are provided to get a map instance and a list instance of an index map.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  IndexMap.ListIndexMap<K,​V>
      Provide access to the index map via list interface.
      static class  IndexMap.MapIndexMap<K,​V>
      Provide access to the index map via map interface.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.List<V> aslist
      The index map as java.util.List.
      protected java.util.Map<K,​V> asmap
      The index map as java.util.Map.
      protected java.util.List<K> list
      The key list.
      protected java.util.Map<K,​V> map
      The key/value map.
    • Constructor Summary

      Constructors 
      Constructor Description
      IndexMap()
      Create a new index map.
      IndexMap​(IndexMap<K,​V> imap)
      Create a new index map.
      IndexMap​(java.util.List<K> list, java.util.Map<K,​V> map)
      Create a new index map using the specified collections as backup.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, java.lang.Object element)
      Unsupported method, due to missing key parameter.
      void add​(int index, K key, V o)
      Add an object to the collection.
      boolean add​(java.lang.Object o)
      Unsupported method, due to missing key parameter.
      void add​(K key, V o)
      Add a new object with key and value.
      boolean addAll​(int index, java.util.Collection c)
      Unsupported method, due to missing key parameter.
      boolean addAll​(java.util.Collection c)
      Unsupported method, due to missing key parameter.
      void clear()
      Removes all mappings from this map.
      java.lang.Object clone()
      Clone an index map.
      boolean contains​(java.lang.Object o)
      Returns
      boolean containsAll​(java.util.Collection<?> c)
      Returns
      boolean containsKey​(java.lang.Object key)
      Returns
      boolean containsValue​(java.lang.Object value)
      Returns
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a set view of the mappings contained in this map.
      boolean equals​(java.lang.Object o)
      Compares the specified object with this map for equality.
      V get​(int index)
      Returns the element at the specified position in this list.
      V get​(java.lang.Object key)
      Returns the value to which this map maps the specified key.
      java.util.List<V> getAsList()
      Return an instance of this index map accessible via list interface.
      java.util.Map<K,​V> getAsMap()
      Return an instance of this index map accessible via map interface.
      K getKey​(int index)
      Get an indexed key.
      java.lang.Object[] getKeys()
      Get the keys as array.
      java.lang.Object[] getKeys​(java.lang.Class<?> type)
      Get the keys as array.
      java.lang.Object[] getObjects()
      Get the values as array.
      java.lang.Object[] getObjects​(java.lang.Class type)
      Get the values as array.
      int hashCode()
      Returns the hash code value for this map.
      int indexOf​(java.lang.Object o)
      Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
      boolean isEmpty()
      Returns
      java.util.Iterator<V> iterator()
      Returns an iterator over the elements in this list in proper sequence.
      java.util.Set<K> keySet()
      Returns a set view of the keys contained in this map.
      int lastIndexOf​(java.lang.Object o)
      Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
      java.util.ListIterator<V> listIterator()
      Returns a list iterator of the elements in this list (in proper sequence).
      java.util.ListIterator<V> listIterator​(int index)
      Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
      V put​(K key, V value)
      Associates the specified value with the specified key in this map (optional operation).
      void putAll​(java.util.Map<? extends K,​? extends V> t)
      Copies all of the mappings from the specified map to this map (optional operation).
      V remove​(int index)
      Removes the element at the specified position in this list.
      boolean removeAll​(java.util.Collection<?> c)
      Removes from this list all the elements that are contained in the specified collection.
      V removeKey​(K key)
      Removes the mapping for this key from this map if it is present.
      boolean removeValue​(java.lang.Object o)
      Removes the first occurrence in this list of the specified element.
      V replace​(K key, V o)
      Replace an object for the given key.
      boolean retainAll​(java.util.Collection<?> c)
      Retains only the elements in this list that are contained in the specified collection.
      V set​(int index, V element)
      Replaces the element at the specified position in this list with the specified element.
      int size()
      Returns the number of key-value mappings in this map.
      java.util.List subList​(int fromIndex, int toIndex)
      Unsupported method.
      java.lang.Object[] toArray()
      Returns an array containing all of the elements in this list in proper sequence.
      <T> T[] toArray​(T[] array)
      Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
      java.lang.String toString()
      Create a string representation of this map.
      java.util.Collection<V> values()
      Returns a collection view of the values contained in this map.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • list

        protected java.util.List<K> list
        The key list.
      • map

        protected java.util.Map<K,​V> map
        The key/value map.
      • asmap

        protected java.util.Map<K,​V> asmap
        The index map as java.util.Map.
      • aslist

        protected java.util.List<V> aslist
        The index map as java.util.List.
    • Constructor Detail

      • IndexMap

        public IndexMap()
        Create a new index map.
      • IndexMap

        public IndexMap​(IndexMap<K,​V> imap)
        Create a new index map.
      • IndexMap

        public IndexMap​(java.util.List<K> list,
                        java.util.Map<K,​V> map)
        Create a new index map using the specified collections as backup.
        Parameters:
        list - The key list.
        map - The key/value map.
    • Method Detail

      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Clone an index map.
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • size

        public int size()
        Returns the number of key-value mappings in this map. If the map contains more than
        Returns:
        the number of key-value mappings in this map.
      • isEmpty

        public boolean isEmpty()
        Returns
        Returns:
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns
        Parameters:
        key - key whose presence in this map is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the key is
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns
        Parameters:
        value - value whose presence in this map is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the value is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the value is
      • get

        public V get​(java.lang.Object key)
        Returns the value to which this map maps the specified key. Returns
        Parameters:
        key - key whose associated value is to be returned.
        Returns:
        the value to which this map maps the specified key, or
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - key is
        See Also:
        containsKey(Object)
      • put

        public V put​(K key,
                     V value)
        Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map m.containsKey(k) would return
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        previous value associated with specified key, or
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
        java.lang.IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
        java.lang.NullPointerException - this map does not permit
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> t)
        Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key
        Parameters:
        t - Mappings to be stored in this map.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map.
        java.lang.IllegalArgumentException - some aspect of a key or value in the specified map prevents it from being stored in this map.
        java.lang.NullPointerException - the specified map is
      • clear

        public void clear()
        Removes all mappings from this map.
      • keySet

        public java.util.Set<K> keySet()
        Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. No key order is reflected in the set!
        Returns:
        a set view of the keys contained in this map.
      • values

        public java.util.Collection<V> values()
        Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined.
        Returns:
        a collection view of the values contained in this map.
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. No key order is reflected in the set!
        Returns:
        a set view of the mappings contained in this map.
      • equals

        public boolean equals​(java.lang.Object o)
        Compares the specified object with this map for equality. Returns
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - object to be compared for equality with this map.
        Returns:
      • hashCode

        public int hashCode()
        Returns the hash code value for this map.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this map.
        See Also:
        Object.hashCode(), Object.equals(Object), equals(Object)
      • toString

        public java.lang.String toString()
        Create a string representation of this map.
        Overrides:
        toString in class java.lang.Object
      • contains

        public boolean contains​(java.lang.Object o)
        Returns
        Parameters:
        o - element whose presence in this list is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the type of the specified element is incompatible with this list (optional).
        java.lang.NullPointerException - if the specified element is null and this list does not support null elements (optional).
      • iterator

        public java.util.Iterator<V> iterator()
        Returns an iterator over the elements in this list in proper sequence.
        Returns:
        an iterator over the elements in this list in proper sequence.
      • toArray

        public java.lang.Object[] toArray()
        Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of the
        Returns:
        an array containing all of the elements in this list in proper sequence.
        See Also:
        Arrays.asList(Object[])
      • toArray

        public <T> T[] toArray​(T[] array)
        Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the
        Parameters:
        array - 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.
        Returns:
        an array containing the elements of this list.
        Throws:
        java.lang.ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
        java.lang.NullPointerException - if the specified array is
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Returns
        Parameters:
        c - collection to be checked for containment in this list.
        Returns:
        Throws:
        java.lang.ClassCastException - if the types of one or more elements in the specified collection are incompatible with this list (optional).
        java.lang.NullPointerException - if the specified collection contains one or more null elements and this list does not support null elements (optional).
        java.lang.NullPointerException - if the specified collection is
        See Also:
        contains(Object)
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Removes from this list all the elements that are contained in the specified collection.
        Parameters:
        c - collection that defines which elements will be removed from this list.
        Returns:
        Throws:
        java.lang.ClassCastException - if the types of one or more elements in this list are incompatible with the specified collection (optional).
        java.lang.NullPointerException - if this list contains one or more null elements and the specified collection does not support null elements (optional).
        java.lang.NullPointerException - if the specified collection is
        See Also:
        removeValue(Object), contains(Object)
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all the elements that are not contained in the specified collection.
        Parameters:
        c - collection that defines which elements this set will retain.
        Returns:
        Throws:
        java.lang.ClassCastException - if the types of one or more elements in this list are incompatible with the specified collection (optional).
        java.lang.NullPointerException - if this list contains one or more null elements and the specified collection does not support null elements (optional).
        java.lang.NullPointerException - if the specified collection is
        See Also:
        removeValue(Object), contains(Object)
      • get

        public V get​(int index)
        Returns the element at the specified position in this list.
        Parameters:
        index - index of element to return.
        Returns:
        the element at the specified position in this list.
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
      • set

        public V set​(int index,
                     V element)
        Replaces the element at the specified position in this list with the specified element.
        Parameters:
        index - index of element to replace.
        element - element to be stored at the specified position.
        Returns:
        the element previously at the specified position.
        Throws:
        java.lang.ClassCastException - if the class of the specified element prevents it from being added to this list.
        java.lang.NullPointerException - if the specified element is null and this list does not support null elements.
        java.lang.IllegalArgumentException - if some aspect of the specified element prevents it from being added to this list.
        java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
      • remove

        public V remove​(int index)
        Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
        Parameters:
        index - the index of the element to removed.
        Returns:
        the element previously at the specified position.
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
      • indexOf

        public int indexOf​(java.lang.Object o)
        Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index
        Parameters:
        o - element to search for.
        Returns:
        the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
        Throws:
        java.lang.ClassCastException - if the type of the specified element is incompatible with this list (optional).
        java.lang.NullPointerException - if the specified element is null and this list does not support null elements (optional).
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index
        Parameters:
        o - element to search for.
        Returns:
        the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
        Throws:
        java.lang.ClassCastException - if the type of the specified element is incompatible with this list (optional).
        java.lang.NullPointerException - if the specified element is null and this list does not support null elements (optional).
      • listIterator

        public java.util.ListIterator<V> listIterator()
        Returns a list iterator of the elements in this list (in proper sequence).
        Returns:
        a list iterator of the elements in this list (in proper sequence).
      • listIterator

        public java.util.ListIterator<V> listIterator​(int index)
        Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to the
        Parameters:
        index - index of first element to be returned from the list iterator (by a call to the
        Returns:
        a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).
      • subList

        public java.util.List subList​(int fromIndex,
                                      int toIndex)
        Unsupported method.
        Throws:
        java.lang.UnsupportedOperationException
      • add

        public boolean add​(java.lang.Object o)
        Unsupported method, due to missing key parameter.
        Throws:
        java.lang.UnsupportedOperationException
      • addAll

        public boolean addAll​(java.util.Collection c)
        Unsupported method, due to missing key parameter.
        Throws:
        java.lang.UnsupportedOperationException
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection c)
        Unsupported method, due to missing key parameter.
        Throws:
        java.lang.UnsupportedOperationException
      • add

        public void add​(int index,
                        java.lang.Object element)
        Unsupported method, due to missing key parameter.
        Throws:
        java.lang.UnsupportedOperationException
      • removeKey

        public V removeKey​(K key)
        Removes the mapping for this key from this map if it is present. More formally, if this map contains a mapping from key (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
        Parameters:
        key - key whose mapping is to be removed from the map.
        Returns:
        previous value associated with specified key, or
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the key is
      • removeValue

        public boolean removeValue​(java.lang.Object o)
        Removes the first occurrence in this list of the specified element. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that
        Parameters:
        o - element to be removed from this list, if present.
        Returns:
        Throws:
        java.lang.ClassCastException - if the type of the specified element is incompatible with this list (optional).
        java.lang.NullPointerException - if the specified element is null and this list does not support null elements (optional).
      • add

        public void add​(K key,
                        V o)
        Add a new object with key and value. The key must not exist.
        Parameters:
        key - The key.
        o - The object.
      • replace

        public V replace​(K key,
                         V o)
        Replace an object for the given key. The key has to exist.
        Parameters:
        key - The key.
        o - The object.
        Returns:
        The old value for the key.
      • getKey

        public K getKey​(int index)
        Get an indexed key.
        Parameters:
        index - The index.
        Returns:
        The key.
      • add

        public void add​(int index,
                        K key,
                        V o)
        Add an object to the collection.
        Parameters:
        index - The index.
        key - The key.
        o - The object.
      • getAsMap

        public java.util.Map<K,​V> getAsMap()
        Return an instance of this index map accessible via map interface.
      • getAsList

        public java.util.List<V> getAsList()
        Return an instance of this index map accessible via list interface.
      • getObjects

        public java.lang.Object[] getObjects()
        Get the values as array.
        Returns:
        The array of values.
      • getObjects

        public java.lang.Object[] getObjects​(java.lang.Class type)
        Get the values as array.
        Parameters:
        type - The component type of the array.
        Returns:
        The array of values.
      • getKeys

        public java.lang.Object[] getKeys()
        Get the keys as array.
        Returns:
        The array of keys.
      • getKeys

        public java.lang.Object[] getKeys​(java.lang.Class<?> type)
        Get the keys as array.
        Parameters:
        type - The component type of the array.
        Returns:
        The array of keys.