E - the type of objects used as keys for the mappublic class ObjectToCounterMap<E> extends HashMap<E,Counter>
ObjectToCounterMap maintains a mapping from objects
to integer counters, which may be incremented or set. Objects not
in the underlying map are assumed to have count 0, so
that incrementing an object that is not mapped sets it to the
increment amount.AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Constructor and Description |
|---|
ObjectToCounterMap()
Construct an object to counter mapping.
|
ObjectToCounterMap(int initialSize)
Construct an object to counter mapping with the specified
initial size.
|
| Modifier and Type | Method and Description |
|---|---|
Comparator<E> |
countComparator()
Returns a
CountComparator that compares objects
based on their counts in this object to counter map. |
int |
getCount(E key)
Returns the current count value for the specified key.
|
void |
increment(E key)
Increment the value of the specified key by
1. |
void |
increment(E key,
int n)
Increment the value of the specified key by the specified
amount.
|
Object[] |
keysOrderedByCount()
Returns the array of keys for this object to counter
map sorted in decreasing order of their value counts.
|
List<E> |
keysOrderedByCountList()
Returns a list of keys for this object to counter map sorted
in descending order of count.
|
void |
prune(int minCount)
Removes all entries in this counter that have less than
the specified minimum count.
|
void |
set(E key,
int n)
Sets the value of the specified key to be the specified amount.
|
String |
toString()
Returns a string representation of this map.
|
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesequals, hashCodepublic ObjectToCounterMap()
public ObjectToCounterMap(int initialSize)
initialSize - Initial size of map.public void increment(E key)
1.
Removes the key from the underlying map if the value after
incrementing is 0. Sets the value of the key to
1 if it is not currently set in the underlying
map.key - Object whose count is incremented by 1public void increment(E key, int n)
0, the key is
removed from the map.key - Object whose count is incremented by the specified
amount.n - Amount to increment the object by.public void set(E key, int n)
0, the key is removed from the mapping.key - Object whose count is incremented by the specified
amount.n - Amount to increment the object by.public int getCount(E key)
0.public String toString()
toString in class AbstractMap<E,Counter>public List<E> keysOrderedByCountList()
public Object[] keysOrderedByCount()
public void prune(int minCount)
minCount - Minimum count of objects to retain.public Comparator<E> countComparator()
CountComparator that compares objects
based on their counts in this object to counter map. It first
compares objects based on their count, and if the counts are
the same and the objects are comparable, on the objects
themselves. Thus compare(obj1,obj2) method will
not be consistent with equals (see Comparable)
unless the set of keys has a natural ordering that is
consistent with equals. This inconsistency with
equals is tolerable in cases where the comparator
is used for sorting, as in Arrays.sort(Object[],Comparator), but not for
backing a sorted collection such as TreeMap
or TreeSet.Copyright © 2019 Alias-i, Inc.. All rights reserved.