Class Frequency<T extends java.lang.Comparable<T>>

java.lang.Object
org.apache.commons.math4.stat.Frequency<T>
Type Parameters:
T - a comparable type used in the frequency distribution
All Implemented Interfaces:
java.io.Serializable

public class Frequency<T extends java.lang.Comparable<T>>
extends java.lang.Object
implements java.io.Serializable
Maintains a frequency distribution.

The values are ordered using the default (natural order), unless a Comparator is supplied in the constructor.

See Also:
Serialized Form
  • Constructor Summary

    Constructors 
    Constructor Description
    Frequency()
    Default constructor.
    Frequency​(java.util.Comparator<T> comparator)
    Constructor allowing values Comparator to be specified.
  • Method Summary

    Modifier and Type Method Description
    void addValue​(T v)
    Adds 1 to the frequency count for v.
    void clear()
    Clears the frequency table
    java.util.Iterator<java.util.Map.Entry<T,​java.lang.Long>> entrySetIterator()
    Return an Iterator over the set of keys and values that have been added.
    boolean equals​(java.lang.Object obj)
    long getCount​(T v)
    Returns the number of values equal to v.
    long getCumFreq​(T v)
    Returns the cumulative frequency of values less than or equal to v.
    double getCumPct​(T v)
    Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).
    java.util.List<T> getMode()
    Returns the mode value(s) in comparator order.
    double getPct​(T v)
    Returns the percentage of values that are equal to v (as a proportion between 0 and 1).
    long getSumFreq()
    Returns the sum of all frequencies.
    int getUniqueCount()
    Returns the number of values in the frequency table.
    int hashCode()
    void incrementValue​(T v, long increment)
    Increments the frequency count for v.
    void merge​(java.util.Collection<Frequency<T>> others)
    Merge a Collection of Frequency objects into this instance.
    void merge​(Frequency<T> other)
    Merge another Frequency object's counts into this instance.
    java.lang.String toString()
    Return a string representation of this frequency distribution.
    java.util.Iterator<T> valuesIterator()
    Returns an Iterator over the set of values that have been added.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Frequency

      public Frequency()
      Default constructor.
    • Frequency

      public Frequency​(java.util.Comparator<T> comparator)
      Constructor allowing values Comparator to be specified.
      Parameters:
      comparator - Comparator used to order values
  • Method Details

    • toString

      public java.lang.String toString()
      Return a string representation of this frequency distribution.
      Overrides:
      toString in class java.lang.Object
      Returns:
      a string representation.
    • addValue

      public void addValue​(T v)
      Adds 1 to the frequency count for v.
      Parameters:
      v - the value to add.
    • incrementValue

      public void incrementValue​(T v, long increment)
      Increments the frequency count for v.
      Parameters:
      v - the value to add.
      increment - the amount by which the value should be incremented
      Since:
      3.1
    • clear

      public void clear()
      Clears the frequency table
    • valuesIterator

      public java.util.Iterator<T> valuesIterator()
      Returns an Iterator over the set of values that have been added.
      Returns:
      values Iterator
    • entrySetIterator

      public java.util.Iterator<java.util.Map.Entry<T,​java.lang.Long>> entrySetIterator()
      Return an Iterator over the set of keys and values that have been added. Using the entry set to iterate is more efficient in the case where you need to access respective counts as well as values, since it doesn't require a "get" for every key...the value is provided in the Map.Entry.
      Returns:
      entry set Iterator
      Since:
      3.1
    • getSumFreq

      public long getSumFreq()
      Returns the sum of all frequencies.
      Returns:
      the total frequency count.
    • getCount

      public long getCount​(T v)
      Returns the number of values equal to v.
      Parameters:
      v - the value to lookup.
      Returns:
      the frequency of v.
    • getUniqueCount

      public int getUniqueCount()
      Returns the number of values in the frequency table.
      Returns:
      the number of unique values that have been added to the frequency table.
      See Also:
      valuesIterator()
    • getPct

      public double getPct​(T v)
      Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

      Returns Double.NaN if no values have been added.

      Parameters:
      v - the value to lookup
      Returns:
      the proportion of values equal to v
    • getCumFreq

      public long getCumFreq​(T v)
      Returns the cumulative frequency of values less than or equal to v.
      Parameters:
      v - the value to lookup.
      Returns:
      the proportion of values equal to v
    • getCumPct

      public double getCumPct​(T v)
      Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).

      Returns Double.NaN if no values have been added.

      Parameters:
      v - the value to lookup
      Returns:
      the proportion of values less than or equal to v
    • getMode

      public java.util.List<T> getMode()
      Returns the mode value(s) in comparator order.
      Returns:
      a list containing the value(s) which appear most often.
      Since:
      3.3
    • merge

      public void merge​(Frequency<T> other) throws NullArgumentException
      Merge another Frequency object's counts into this instance. This Frequency's counts will be incremented (or set when not already set) by the counts represented by other.
      Parameters:
      other - the other Frequency object to be merged
      Throws:
      NullArgumentException - if other is null
      Since:
      3.1
    • merge

      public void merge​(java.util.Collection<Frequency<T>> others) throws NullArgumentException
      Merge a Collection of Frequency objects into this instance. This Frequency's counts will be incremented (or set when not already set) by the counts represented by each of the others.
      Parameters:
      others - the other Frequency objects to be merged
      Throws:
      NullArgumentException - if the collection is null
      Since:
      3.1
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object obj)
      Overrides:
      equals in class java.lang.Object