Class ValueCounter<K>

  • Type Parameters:
    K - the type of value to count

    public class ValueCounter<K>
    extends java.lang.Object
    Simple class to map a key to a count. No space is taken in the map by zero-count items.

    This class is thread-safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      ValueCounter()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the value counts
      int decrement​(K key)
      Decrement the count of a particular key.
      int getCount​(K key)
      Get the count for a given key or 0 if not present.
      K getMaxValue()
      Gets the key that has been seen the most, if there is a tie, the key added first wins.
      int increment​(K key)
      Increment the count of a particular key.
      boolean isEmpty()  
      int keyCount()  
      java.util.Set<K> keySet()
      This method is safe to call at any time.
      void remove​(K key)
      Removes the value
      int size()  
      K[] toArray​(K[] template)  
      java.lang.String toString()
      int update​(K key, int val)
      Update the count of a particular key by val.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ValueCounter

        public ValueCounter()
    • Method Detail

      • increment

        public int increment​(K key)
        Increment the count of a particular key. The key need not already exist.
        Returns:
        the new value
      • decrement

        public int decrement​(K key)
        Decrement the count of a particular key. If the count becomes 0, the key is removed.
        Returns:
        the new value
      • update

        public int update​(K key,
                          int val)
        Update the count of a particular key by val. If the count becomes 0, the key is removed.
        Parameters:
        key -
        val -
        Returns:
        the new value
      • remove

        public void remove​(K key)
        Removes the value
      • getCount

        public int getCount​(K key)
        Get the count for a given key or 0 if not present.
      • getMaxValue

        public K getMaxValue()
        Gets the key that has been seen the most, if there is a tie, the key added first wins. NOTE, this method does not reflect removal of a key from the map (if the maximum value key is removed, this method will still indicate it is the maximum).
      • isEmpty

        public boolean isEmpty()
      • keyCount

        public int keyCount()
        Returns:
        the number of keys with non-zero values
      • toArray

        public K[] toArray​(K[] template)
        Parameters:
        template -
        Returns:
        an array of the keys
      • keySet

        public java.util.Set<K> keySet()
        This method is safe to call at any time. The returned value is a snapshot and copy of the the keys in place when called.
        Returns:
        the set of keys which have non-zero values.
      • size

        public int size()
        Returns:
        number of non-zero values
      • clear

        public void clear()
        Clears the value counts
      • toString

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