public class CounterSet<E> extends java.lang.Object implements java.io.Serializable, java.lang.Iterable<Pair<E,java.lang.Integer>>
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<E,java.lang.Integer> |
map
The underlying map.
|
protected int |
total
Stores total value.
|
Constructor and Description |
---|
CounterSet()
Constructs an empty
CounterSet . |
CounterSet(java.util.Collection<E> keys)
Constructs a new
CounterSet from the given keys. |
CounterSet(E key,
int value)
Constructs a CounterSet with one value.
|
Modifier and Type | Method and Description |
---|---|
void |
add(CounterSet<E> other)
Adds the given
CounterSet to this CounterSet by incrementing
all keys contained from other. |
void |
clear()
Clears the counter set.
|
boolean |
contains(E key)
Checks if an element is stored in the array.
|
boolean |
equals(java.lang.Object obj) |
UnmodifiableSet<E> |
getKeys()
Returns the set of all elements used a keys for counters.
|
java.util.List<E> |
getKeysByValueAscending()
Returns a list of all keys ordered by their value ascending
|
java.util.List<E> |
getKeysByValueDescending()
Returns a list of all keys ordered by their value descending
|
int |
getTotal()
Get total sum of all elements.
|
int |
getValue(E key)
Get the value for an element.
|
int |
hashCode() |
int |
inc(E key)
Same as
inc(key, 1) . |
int |
inc(E key,
int increment)
Add the given increment to an element.
|
void |
incAll(java.util.Collection<E> keys)
Increments the given elements by 1
|
void |
incAll(java.util.Collection<E> keys,
int increment)
Add the given increment to the given keys.
|
java.util.Iterator<Pair<E,java.lang.Integer>> |
iterator() |
void |
printValueDistribution(boolean ascending)
Prints the distribution of values (ascending or descending) to System.out,
where each value is printed on a separate line in the form <key> :
<value>.
|
void |
printValueDistribution(java.io.PrintWriter writer,
boolean ascending)
Prints the distribution of values (ascending or descending) to the given
stream, where each value is printed on a separate line in the form
<key> : <value>.
|
void |
remove(E key)
Remove the entry with the given key, i.e.
|
void |
removeAll(java.util.Collection<E> keys)
Removes all entries with the given keys.
|
void |
removeIf(java.util.function.BiFunction<E,java.lang.Integer,java.lang.Boolean> filter)
Removes all keys that the given filter matches.
|
static <E> CounterSet<E> |
removeSecondFromFirst(CounterSet<E> first,
CounterSet<E> second)
Removes the second CounterSet from the first one and returns a new
CounterSet.
|
java.util.Map<E,java.lang.Integer> |
toMap()
Returns the values as
Map . |
java.util.Map<E,java.lang.Integer> |
toMapWithoutZeroEntries()
Returns the values as a
Map , but omits keys where the value is 0. |
java.lang.String |
toString() |
java.util.Collection<java.lang.Integer> |
values()
Returns a collection of all values
|
protected int total
public CounterSet()
CounterSet
.public CounterSet(java.util.Collection<E> keys)
CounterSet
from the given keys. Initializes all keys
with 1.public CounterSet(E key, int value)
public int inc(E key, int increment)
key
- the key of the counter to increment.increment
- the increment.public int inc(E key)
inc(key, 1)
.inc(Object, int)
public void incAll(java.util.Collection<E> keys, int increment)
keys
- the keys of the counter to increment.increment
- the increment.public void add(CounterSet<E> other)
CounterSet
to this CounterSet
by incrementing
all keys contained from other.public static <E> CounterSet<E> removeSecondFromFirst(CounterSet<E> first, CounterSet<E> second)
public void removeIf(java.util.function.BiFunction<E,java.lang.Integer,java.lang.Boolean> filter)
public void remove(E key)
public void removeAll(java.util.Collection<E> keys)
public void clear()
public int getValue(E key)
0
is returned.public UnmodifiableSet<E> getKeys()
public java.util.List<E> getKeysByValueAscending()
public java.util.List<E> getKeysByValueDescending()
public int getTotal()
public java.util.Collection<java.lang.Integer> values()
public java.lang.String toString()
toString
in class java.lang.Object
public void printValueDistribution(boolean ascending)
Example:
foo : 4
bar : 2
public void printValueDistribution(java.io.PrintWriter writer, boolean ascending)
Example:
foo : 4
bar : 2
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.util.Map<E,java.lang.Integer> toMapWithoutZeroEntries()
Map
, but omits keys where the value is 0.