java有频率表吗? [英] Does java have a Frequency table?

查看:53
本文介绍了java有频率表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 有内置频率表吗?我记得在我的一个课程中使用过一个,我知道 Python 有一个,但我不记得是我自己构建了一个还是 Java 有一个可供使用.

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

或者在递增时:

freq.put(value, freq.get(value) + 1); // always works, won't throw NPE etc

这篇关于java有频率表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆