K
- public class CountingMap<K> extends Object implements Map<K,Integer>
This replaces the idiom of map.containsKey(k) ? map.put(k, map.get(k) + 1) : map.put(k, 0);
Constructor and Description |
---|
CountingMap()
Constructs a CountingMap backed by a simple HashMap.
|
CountingMap(Map<K,Integer> map)
Constructs a CountingMap backed by the specified Map.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
int |
count(K key)
Returns the count associated with the specified key, or zero if the key has never been incremented.
|
Set<Map.Entry<K,Integer>> |
entrySet() |
boolean |
equals(Object o) |
Integer |
get(Object key) |
int |
hashCode() |
int |
increment(K key)
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() |
Set<K> |
keySet() |
int |
max() |
Integer |
put(K key,
Integer value) |
void |
putAll(Map<? extends K,? extends Integer> t) |
Integer |
remove(Object key) |
boolean |
seen(K key)
Returns true if the specified key has ever been incremented, false otherwise.
|
int |
size() |
List<K> |
sortedKeyList()
Returns a list of the keys in this map, sorted by ascending count.
|
List<K> |
sortedKeyList(boolean sortDescending)
Returns a list of the keys in this map, sorted as specified.
|
String |
toString() |
int |
total()
Returns the sum of all counts in the map.
|
Collection<Integer> |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public CountingMap()
public boolean containsKey(Object key)
containsKey
in interface Map<K,Integer>
public boolean containsValue(Object value)
containsValue
in interface Map<K,Integer>
public int count(K key)
key
- the key whose associated count is to be returnedpublic boolean equals(Object o)
public int hashCode()
public int increment(K key)
key
- the key whose associated count is to be incrementedpublic void incrementAll(Collection<K> keys)
keys
- the keys whose associated count is to be incrementedpublic int max()
public boolean seen(K key)
key
- the key whose presence is to be testedpublic List<K> sortedKeyList()
public List<K> sortedKeyList(boolean sortDescending)
sortDescending
- true to sort by descending count, false to sort by ascending countpublic int total()
Copyright © 2003–2022 UBC Michael Smith Laboratories. All rights reserved.