Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java have a Frequency table?

Tags:

java

object

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.

like image 507
Austin Johnston Avatar asked Jun 05 '26 11:06

Austin Johnston


2 Answers

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
like image 92
Bohemian Avatar answered Jun 07 '26 00:06

Bohemian


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.

like image 42
fabiim Avatar answered Jun 07 '26 01:06

fabiim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!