Class ListMap<K,​V>

  • Type Parameters:
    K - the class of keys for this map.
    V - the class of values for this map.
    All Implemented Interfaces:
    java.util.Map<K,​V>, java.util.SortedMap<K,​V>

    public final class ListMap<K,​V>
    extends java.lang.Object
    implements java.util.SortedMap<K,​V>
    A SortedMap with ordering given by the ordering by which the keys are added. This is the Map corresponding with ListSet.

    Did not use AbstractMap, e.g. because keySet() shall return more than just a Set.

    Version:
    1.0
    Author:
    Ernst Reissner
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.SortedSet<java.util.Map.Entry<K,​V>> entrySet
      The set of entries of this map returned by entrySet().
      private ListSet<K> keys
      The set of keys of this ListMap.
      private java.util.SortedSet<K> keysSet
      The set of keys of this map returned by keySet().
      private java.util.List<V> values
      The values of this map.
      private java.util.Collection<V> valuesColl
      The collection of values of this map returned by values().
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ListMap()
      Creates an empty list map with key set 'ordered as added' as described by ListSet.sortedAsAdded().
      private ListMap​(ListSet<K> keys, java.util.List<V> values)
      Creates a list map defined by the given keys and values keys and values.
        ListMap​(java.util.Map<K,​V> map)
      Creates an list map with the same key-value pairs as map and with ordering 'as added'.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      java.util.Comparator<? super K> comparator()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object val)  
      java.util.SortedSet<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object obj)  
      K firstKey()  
      V get​(java.lang.Object key)  
      int hashCode()  
      ListMap<K,​V> headMap​(K toKey)  
      boolean isEmpty()  
      java.util.SortedSet<K> keySet()  
      K lastKey()  
      static void main​(java.lang.String[] args)  
      private static <K,​V>
      ListSet<K>
      map2keys​(java.util.Map<K,​V> map)
      Returns the key set of map as a ListSet with ordering 'ordered as added' as given by ListSet.sortedAsAdded().
      private static <K,​V>
      java.util.List<V>
      map2values​(java.util.Map<K,​V> map)
      Returns the value collection of map as a List with ordering given by the iteration on the key set of map.
      V put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> other)  
      V remove​(java.lang.Object key)  
      private boolean removeIdx​(int idx)  
      int size()  
      (package private) ListMap<K,​V> subMap​(int fromIdx, int toIdx)  
      ListMap<K,​V> subMap​(K fromKey, K toKey)  
      ListMap<K,​V> tailMap​(K fromKey)  
      java.lang.String toString()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • keys

        private final ListSet<K> keys
        The set of keys of this ListMap.
      • values

        private final java.util.List<V> values
        The values of this map. The according keys are in keys.
      • valuesColl

        private final java.util.Collection<V> valuesColl
        The collection of values of this map returned by values(). Note that each ListMap has a single values collection.
      • keysSet

        private final java.util.SortedSet<K> keysSet
        The set of keys of this map returned by keySet(). Note that each ListMap has a single key set.
      • entrySet

        private final java.util.SortedSet<java.util.Map.Entry<K,​V>> entrySet
        The set of entries of this map returned by entrySet(). Note that each ListMap has a single entry set.
    • Constructor Detail

      • ListMap

        public ListMap()
        Creates an empty list map with key set 'ordered as added' as described by ListSet.sortedAsAdded().
      • ListMap

        public ListMap​(java.util.Map<K,​V> map)
        Creates an list map with the same key-value pairs as map and with ordering 'as added'. If no additional keys are added, this reflects the ordering given by iterating through the key set of map.
        See Also:
        map2keys(Map), map2values(Map)
      • ListMap

        private ListMap​(ListSet<K> keys,
                        java.util.List<V> values)
        Creates a list map defined by the given keys and values keys and values. A key and a value correspond iff their index coincides. Thus keys and values must have the same size. Note that the entries of keys differ pairwise, whereas this is not necessary for values.
        Parameters:
        keys - the set of keys as a ListSet.
        values - the collection of according values as a List with the same size as keys.
        Throws:
        java.lang.IllegalArgumentException - if keys and values differ in size.
    • Method Detail

      • map2keys

        private static <K,​V> ListSet<K> map2keys​(java.util.Map<K,​V> map)
        Returns the key set of map as a ListSet with ordering 'ordered as added' as given by ListSet.sortedAsAdded(). This ordering reflects the ordering given by the iteration on the key set of map.
      • map2values

        private static <K,​V> java.util.List<V> map2values​(java.util.Map<K,​V> map)
        Returns the value collection of map as a List with ordering given by the iteration on the key set of map. Note that there is no documentation **** that this ordering is the same as the one of the value collection map#values(). ****
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object val)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • entrySet

        public java.util.SortedSet<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Specified by:
        entrySet in interface java.util.SortedMap<K,​V>
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • keySet

        public java.util.SortedSet<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Specified by:
        keySet in interface java.util.SortedMap<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> other)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • removeIdx

        private boolean removeIdx​(int idx)
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
      • comparator

        public java.util.Comparator<? super K> comparator()
        Specified by:
        comparator in interface java.util.SortedMap<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
        Specified by:
        values in interface java.util.SortedMap<K,​V>
      • firstKey

        public K firstKey()
        Specified by:
        firstKey in interface java.util.SortedMap<K,​V>
      • lastKey

        public K lastKey()
        Specified by:
        lastKey in interface java.util.SortedMap<K,​V>
      • headMap

        public ListMap<K,​V> headMap​(K toKey)
        Specified by:
        headMap in interface java.util.SortedMap<K,​V>
      • tailMap

        public ListMap<K,​V> tailMap​(K fromKey)
        Specified by:
        tailMap in interface java.util.SortedMap<K,​V>
      • subMap

        ListMap<K,​V> subMap​(int fromIdx,
                                  int toIdx)
      • subMap

        public ListMap<K,​V> subMap​(K fromKey,
                                         K toKey)
        Specified by:
        subMap in interface java.util.SortedMap<K,​V>
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • main

        public static void main​(java.lang.String[] args)