Package ubic.basecode.dataStructure
Class CountingMap<K>
java.lang.Object
ubic.basecode.dataStructure.CountingMap<K>
- Type Parameters:
K
-
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
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a CountingMap backed by a simple HashMap.CountingMap
(Map<K, Integer> map) Constructs a CountingMap backed by the specified Map. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) int
Returns the count associated with the specified key, or zero if the key has never been incremented.entrySet()
boolean
int
hashCode()
int
Increments the count associated with the specified key and returns the incremented count.void
incrementAll
(Collection<K> keys) Increments the count associated with the specified keys.boolean
isEmpty()
keySet()
int
max()
void
boolean
Returns true if the specified key has ever been incremented, false otherwise.int
size()
Returns a list of the keys in this map, sorted by ascending count.sortedKeyList
(boolean sortDescending) Returns a list of the keys in this map, sorted as specified.toString()
int
total()
Returns the sum of all counts in the map.values()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
CountingMap
public CountingMap()Constructs a CountingMap backed by a simple HashMap. -
CountingMap
Constructs a CountingMap backed by the specified Map.- Parameters:
map
- the backing Map
-
-
Method Details
-
clear
public void clear() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
Integer>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
Integer>
-
count
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
-
equals
-
get
-
hashCode
public int hashCode() -
increment
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
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() -
keySet
-
max
public int max()- Returns:
-
put
-
putAll
-
remove
-
seen
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() -
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
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
-
total
public int total()Returns the sum of all counts in the map. -
values
-