Class CountingMap<K>

java.lang.Object
ubic.basecode.dataStructure.CountingMap<K>
Type Parameters:
K -
All Implemented Interfaces:
Map<K,Integer>

public class CountingMap<K> extends Object implements Map<K,Integer>
Convenience Map that has a count as the value for each key. Calling increment(K key) increases the count for key.

This replaces the idiom of map.containsKey(k) ? map.put(k, map.get(k) + 1) : map.put(k, 0);

Author:
luke
  • Constructor Details

    • CountingMap

      public CountingMap()
      Constructs a CountingMap backed by a simple HashMap.
    • CountingMap

      public CountingMap(Map<K,Integer> map)
      Constructs a CountingMap backed by the specified Map.
      Parameters:
      map - the backing Map
  • Method Details

    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,Integer>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,Integer>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,Integer>
    • count

      public int count(K key)
      Returns the count associated with the specified key, or zero if the key has never been incremented.
      Parameters:
      key - the key whose associated count is to be returned
      Returns:
      the count associated with the specified key, or zero if the key has never been incremented
    • entrySet

      public Set<Map.Entry<K,Integer>> entrySet()
      Specified by:
      entrySet in interface Map<K,Integer>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<K,Integer>
      Overrides:
      equals in class Object
    • get

      public Integer get(Object key)
      Specified by:
      get in interface Map<K,Integer>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,Integer>
      Overrides:
      hashCode in class Object
    • increment

      public int increment(K key)
      Increments the count associated with the specified key and returns the incremented count. If the key doesn't already exist in the map, it will be added.
      Parameters:
      key - the key whose associated count is to be incremented
      Returns:
      the incremented value associated with the specified key
    • incrementAll

      public void incrementAll(Collection<K> keys)
      Increments the count associated with the specified keys. If a key doesn't already exist in the map, it will be added.
      Parameters:
      keys - the keys whose associated count is to be incremented
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,Integer>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,Integer>
    • max

      public int max()
      Returns:
    • put

      public Integer put(K key, Integer value)
      Specified by:
      put in interface Map<K,Integer>
    • putAll

      public void putAll(Map<? extends K,? extends Integer> t)
      Specified by:
      putAll in interface Map<K,Integer>
    • remove

      public Integer remove(Object key)
      Specified by:
      remove in interface Map<K,Integer>
    • seen

      public boolean seen(K key)
      Returns true if the specified key has ever been incremented, false otherwise.
      Parameters:
      key - the key whose presence is to be tested
      Returns:
      true if the specified key has ever been incremented, false otherwise
    • size

      public int size()
      Specified by:
      size in interface Map<K,Integer>
    • sortedKeyList

      public List<K> sortedKeyList()
      Returns a list of the keys in this map, sorted by ascending count.
      Returns:
      a list of the keys in this map, sorted by ascending count
    • sortedKeyList

      public List<K> sortedKeyList(boolean sortDescending)
      Returns a list of the keys in this map, sorted as specified.
      Parameters:
      sortDescending - true to sort by descending count, false to sort by ascending count
      Returns:
      a list of the keys in this map, sorted as specified.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • total

      public int total()
      Returns the sum of all counts in the map.
    • values

      public Collection<Integer> values()
      Specified by:
      values in interface Map<K,Integer>