Does Java have a built in Frequency Table? I remember using one in one of my classes and I know Python has one, but I do not remember if I built one on my own or if Java has one for use.
I assume by "frequency table" you mean a map of value-to-count.
The answer is no.
It's not much of a stretch to create a well-behaved one though:
Map<T, Integer> freq = new HashMap<T, Integer> () {
@Override
public Integer get(Object key) {
return containsKey(key) ? super.get(key) : 0;
}
}
Then when using you don't have to clutter your code with null checks when getting a frequency:
freq.get(value); // returns zero for not found
Or when incrementing:
freq.put(value, freq.get(value) + 1); // always works, won't throw NPE etc
Not built-in but you can check Apache Commons Statistics. It has an easy Frequency table builder that allows to find frequency, cumulative frequency, counts, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With