Package com.gengoai.collection.counter
Interface Counter<T>
-
- Type Parameters:
T
- Component type being counted.
- All Known Implementing Classes:
BaseMapCounter
,ConcurrentHashMapCounter
,HashMapCounter
public interface Counter<T> extends Copyable<Counter<T>>
A specialized object to double map that allows basic statistics over the objects and their values.
- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Counter<T>
adjustValues(DoubleUnaryOperator function)
Constructs a new counter made up of counts that are adjusted using the supplied function.Counter<T>
adjustValuesSelf(DoubleUnaryOperator function)
Adjust the values in-place using the supplied functionMap<T,Double>
asMap()
Provides a map view of this counterdefault double
average()
Calculates the average of the values in the counterCounter<T>
bottomN(int n)
Creates a new counter containing the N items with lowest valuesvoid
clear()
Clears the counterboolean
contains(T item)
Determines if the item is in the counterdefault Counter<T>
decrement(T item)
Decrements the count of the item by one.default Counter<T>
decrement(T item, double amount)
Decrements the count of the item by a given amountdefault Counter<T>
decrementAll(Iterable<? extends T> iterable)
Decrements all items in a given iterable by 1default Counter<T>
decrementAll(Iterable<? extends T> iterable, double amount)
Decrements all items in a given iterable by a given amountdefault Counter<T>
divideBySum()
Divides the values in the counter by the sum and sets the sum to 1.0Set<Map.Entry<T,Double>>
entries()
A set of object - double entries making up the counterCounter<T>
filterByKey(Predicate<? super T> predicate)
Creates a new counter containing only those items that evaluate true for the given predicateCounter<T>
filterByValue(DoublePredicate doublePredicate)
Creates a new counter containing only those items whose value evaluate true for the given predicatedefault void
forEach(BiConsumer<? super T,? super Double> consumer)
Convenience method for consuming item - count pairs.double
get(T item)
Returns the value for the given itemdefault Counter<T>
increment(T item)
Increments the count of the item by one.Counter<T>
increment(T item, double amount)
Increments the count of the item by a given amountdefault Counter<T>
incrementAll(Iterable<? extends T> iterable)
Increments all items in a given iterable by 1default Counter<T>
incrementAll(Iterable<? extends T> iterable, double amount)
Increments all items in a given iterable by a given amountboolean
isEmpty()
Determines if the counter is empty or notSet<T>
items()
The items in the counterdefault List<T>
itemsByCount(boolean ascending)
Returns the items as a sorted list by their counts.default double
magnitude()
Calculates the magnitude (square root of sum of squares) of the items in the Counter.<R> Counter<R>
mapKeys(Function<? super T,? extends R> function)
Creates a new counter by mapping the items of this counter using the supplied functiondefault T
max()
Determines the item with the maximum value in the counterdefault double
maximumCount()
Determines the maximum value in the counterCounter<T>
merge(Counter<? extends T> other)
Merges the counts in one counter with this one.Counter<T>
merge(Map<? extends T,? extends Number> other)
Merges the counts in a map with this counter.default T
min()
Determines the item with the minimum value in the counterdefault double
minimumCount()
Determines the minimum value in the counterdouble
remove(T item)
Removes an item from the counterCounter<T>
removeAll(Iterable<T> items)
Removes all the given items from the counterdefault T
sample()
Sample an item based on its count.default T
sample(Random rnd)
Sample an item based on its count.Counter<T>
set(T item, double count)
Sets the value of an item in the counterint
size()
The total number of items in the counterdefault double
standardDeviation()
Calculates the standard deviation of the items in the counterdouble
sum()
The sum of values in the counterCounter<T>
topN(int n)
Creates a new counter containing the N items with highest valuesCollection<Double>
values()
The values associated with the items in the counterdefault void
writeCsv(Resource output)
Writes the counter items and values to CSV
-
-
-
Method Detail
-
adjustValues
default Counter<T> adjustValues(DoubleUnaryOperator function)
Constructs a new counter made up of counts that are adjusted using the supplied function.- Parameters:
function
- The function to use to adjust the counts- Returns:
- The new counter with adjusted counts.
-
adjustValuesSelf
Counter<T> adjustValuesSelf(DoubleUnaryOperator function)
Adjust the values in-place using the supplied function- Parameters:
function
- The function to use to adjust the counts- Returns:
- this counter
-
average
default double average()
Calculates the average of the values in the counter- Returns:
- The average count in the counter
-
bottomN
Counter<T> bottomN(int n)
Creates a new counter containing the N items with lowest values- Parameters:
n
- the number of items desired- Returns:
- a counter containing the N items with lowest values from this counter
-
clear
void clear()
Clears the counter
-
contains
boolean contains(T item)
Determines if the item is in the counter- Parameters:
item
- item to check- Returns:
- True if item is in the counter, false otherwise
-
decrement
default Counter<T> decrement(T item)
Decrements the count of the item by one.- Parameters:
item
- The item to increment- Returns:
- the counter
-
decrement
default Counter<T> decrement(T item, double amount)
Decrements the count of the item by a given amount- Parameters:
item
- The item to incrementamount
- The amount to decrement- Returns:
- the counter
-
decrementAll
default Counter<T> decrementAll(Iterable<? extends T> iterable)
Decrements all items in a given iterable by 1- Parameters:
iterable
- The iterable of items to decrement- Returns:
- the counter
-
decrementAll
default Counter<T> decrementAll(Iterable<? extends T> iterable, double amount)
Decrements all items in a given iterable by a given amount- Parameters:
iterable
- The iterable of items to decrementamount
- The amount to decrement- Returns:
- the counter
-
divideBySum
default Counter<T> divideBySum()
Divides the values in the counter by the sum and sets the sum to 1.0- Returns:
- the counter
-
entries
Set<Map.Entry<T,Double>> entries()
A set of object - double entries making up the counter- Returns:
- the set of entries
-
filterByKey
Counter<T> filterByKey(Predicate<? super T> predicate)
Creates a new counter containing only those items that evaluate true for the given predicate- Parameters:
predicate
- the predicate to use to filter the keys- Returns:
- A new counter containing only those items that evaluate true for the given predicate
-
filterByValue
Counter<T> filterByValue(DoublePredicate doublePredicate)
Creates a new counter containing only those items whose value evaluate true for the given predicate- Parameters:
doublePredicate
- the predicate to use to filter the values- Returns:
- A new counter containing only those items whose value evaluate true for the given predicate
-
forEach
default void forEach(BiConsumer<? super T,? super Double> consumer)
Convenience method for consuming item - count pairs.- Parameters:
consumer
- the consumer to use for processing the key value pairs
-
get
double get(T item)
Returns the value for the given item- Parameters:
item
- The item we want the count for- Returns:
- The value of the item or 0 if it is not in the counter.
-
increment
default Counter<T> increment(T item)
Increments the count of the item by one.- Parameters:
item
- The item to increment- Returns:
- the counter
-
increment
Counter<T> increment(T item, double amount)
Increments the count of the item by a given amount- Parameters:
item
- The item to incrementamount
- The amount to increment- Returns:
- the counter
-
incrementAll
default Counter<T> incrementAll(Iterable<? extends T> iterable)
Increments all items in a given iterable by 1- Parameters:
iterable
- The iterable of items to increment- Returns:
- the counter
-
incrementAll
default Counter<T> incrementAll(Iterable<? extends T> iterable, double amount)
Increments all items in a given iterable by a given amount- Parameters:
iterable
- The iterable of items to incrementamount
- The amount to increment- Returns:
- the counter
-
isEmpty
boolean isEmpty()
Determines if the counter is empty or not- Returns:
- True if the counter is empty
-
itemsByCount
default List<T> itemsByCount(boolean ascending)
Returns the items as a sorted list by their counts.- Parameters:
ascending
- True if the counts are sorted in ascending order, False if in descending order.- Returns:
- The sorted list of items.
-
magnitude
default double magnitude()
Calculates the magnitude (square root of sum of squares) of the items in the Counter.- Returns:
- the magnitude
-
mapKeys
<R> Counter<R> mapKeys(Function<? super T,? extends R> function)
Creates a new counter by mapping the items of this counter using the supplied function- Type Parameters:
R
- The component type of the new counter- Parameters:
function
- the function to use to transform the keys- Returns:
- A new counter containing only those items that evaluate true for the given predicate
-
max
default T max()
Determines the item with the maximum value in the counter- Returns:
- The item with max count
-
maximumCount
default double maximumCount()
Determines the maximum value in the counter- Returns:
- The maximum count in the counter
-
merge
Counter<T> merge(Counter<? extends T> other)
Merges the counts in one counter with this one.- Parameters:
other
- The other counter to merge.- Returns:
- the counter
-
merge
Counter<T> merge(Map<? extends T,? extends Number> other)
Merges the counts in a map with this counter.- Parameters:
other
- The other counter to merge.- Returns:
- the counter
-
min
default T min()
Determines the item with the minimum value in the counter- Returns:
- The item with min count
-
minimumCount
default double minimumCount()
Determines the minimum value in the counter- Returns:
- The minimum count in the counter
-
remove
double remove(T item)
Removes an item from the counter- Parameters:
item
- The item to remove- Returns:
- the count of the removed item
-
removeAll
Counter<T> removeAll(Iterable<T> items)
Removes all the given items from the counter- Parameters:
items
- The items to remove- Returns:
- the counter
-
sample
default T sample(Random rnd)
Sample an item based on its count.- Parameters:
rnd
- The random number generator- Returns:
- the sampled item
-
sample
default T sample()
Sample an item based on its count.- Returns:
- the sampled item
-
set
Counter<T> set(T item, double count)
Sets the value of an item in the counter- Parameters:
item
- The itemcount
- The count- Returns:
- the counter
-
size
int size()
The total number of items in the counter- Returns:
- The number of items in the counter
-
standardDeviation
default double standardDeviation()
Calculates the standard deviation of the items in the counter- Returns:
- The standard deviation of the counts in the counter
-
sum
double sum()
The sum of values in the counter- Returns:
- The sum of the counts in the counter
-
topN
Counter<T> topN(int n)
Creates a new counter containing the N items with highest values- Parameters:
n
- the number of items desired- Returns:
- a counter containing the N items with highest values from this counter
-
values
Collection<Double> values()
The values associated with the items in the counter- Returns:
- The values of the items in the counter.
-
writeCsv
default void writeCsv(Resource output) throws IOException
Writes the counter items and values to CSV- Parameters:
output
- the resource to write to- Throws:
IOException
- Something went wrong writing
-
-