net.sf.statcvs.util
Class IntegerMap
Utility class for storing a map from
Object
s to
int
s.
This class makes it easy to sort by key or value, and provides
useful features like
sum()
,
max()
, and
percent calculation.
The keys must be comparable, for example
String
s.
Behaviour for
null
keys is unspecified.
$Id: IntegerMap.java,v 1.16 2008/04/02 11:52:02 benoitx Exp $ void | addInt(Object key, int addValue) - Adds an
int to the value stored at a key.
|
double | average() - Returns the average of all values in the map.
|
boolean | contains(Object key) - Returns
true if the map contains a value
for this key.
|
void | dec(Object key) - Same as
addInt(key, -1)
|
int | get(Object key) - Gets a value from the map.
|
Integer | getInteger(Object key) - Same as
get(Object) , but returns an Integer ,
not an int .
|
double | getPercent(Object key) - Gets the value stored at a key as a percentage of all values
in the map.
|
double | getPercentOfMaximum(Object key) - Gets the value stored at a key as a percentage of the maximum
value in the map.
|
void | inc(Object key) - Same as
addInt(key, 1)
|
Iterator | iteratorSortedByKey() - Returns an iterator on the keys, sorted by key ascending.
|
Iterator | iteratorSortedByValue() - Returns an iterator on the keys, sorted by values ascending.
|
Iterator | iteratorSortedByValueReverse() - Returns an iterator on the keys, sorted by values descending.
|
Set | keySet() - Returns a set view of the keys.
|
int | max() - Returns the maximum value in the map.
|
void | put(Object key, int value) - Puts a value into the map, overwriting any previous value
for the same key.
|
void | remove(Object key) - Deletes a value from the map.
|
int | size() - Returns the number of key-value pairs stored in the map.
|
int | sum() - Returns the sum of all values in the map.
|
addInt
public void addInt(Object key,
int addValue)
Adds an int
to the value stored at a key.
If no value was stored before at this key, the int
will be stored there.
key
- the key to whose value addValue
should be addedaddValue
- the int
to be added
average
public double average()
Returns the average of all values in the map.
- the average of all values in the map
contains
public boolean contains(Object key)
Returns true
if the map contains a value
for this key.
key
- the key to check for
true
if the key is in the map
dec
public void dec(Object key)
Same as addInt(key, -1)
key
- the key whose value should be decreased
get
public int get(Object key)
Gets a value from the map. Returns the value which was
stored in the map at the same key before. If no value was
stored for this key, 0 will be returned.
key
- an Object
which is used as key.
getInteger
public Integer getInteger(Object key)
Same as
get(Object)
, but returns an
Integer
,
not an
int
.
key
- the key to get the value for
- the value wrapped in an
Integer
object
getPercent
public double getPercent(Object key)
Gets the value stored at a key as a percentage of all values
in the map.
key
- the key to get the value for
- the value as a percentage of the sum of all values
getPercentOfMaximum
public double getPercentOfMaximum(Object key)
Gets the value stored at a key as a percentage of the maximum
value in the map. For the maximum value, this will return
100.0. For a value half as large as the maximum value, this
will return 50.0.
key
- the key to get the value for
- the value as a percentage of largest value in the map
inc
public void inc(Object key)
Same as addInt(key, 1)
key
- the key whose value should be increased
iteratorSortedByKey
public Iterator iteratorSortedByKey()
Returns an iterator on the keys, sorted by key ascending.
iteratorSortedByValue
public Iterator iteratorSortedByValue()
Returns an iterator on the keys, sorted by values ascending.
iteratorSortedByValueReverse
public Iterator iteratorSortedByValueReverse()
Returns an iterator on the keys, sorted by values descending.
keySet
public Set keySet()
Returns a set view of the keys. The set will be in
ascending key order.
max
public int max()
Returns the maximum value in the map.
- the maximum value in the map.
put
public void put(Object key,
int value)
Puts a value into the map, overwriting any previous value
for the same key.
key
- an Object
which is used as key.value
- the int
value to be stored at this key.
remove
public void remove(Object key)
Deletes a value from the map. This is different from
put(key, 0)
. Removing will reduce
the size of the map, putting 0 will not.
key
- the key that should be removed
size
public int size()
Returns the number of key-value pairs stored in the map.
- the number of key-value pairs stored in the map
sum
public int sum()
Returns the sum of all values in the map.
- the sum of all values in the map