在Java中映射中计算密钥的出现次数 [英] Counting occurrences of a key in a Map in Java

查看:129
本文介绍了在Java中映射中计算密钥的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个从.java文件捕获Java关键字的项目,并使用地图跟踪事件。我过去曾经使用过类似的方法,但是我似乎没有在这里使用这种方法。

  Map< String,Integer> map = new TreeMap< String,Integer>(); 
设置< String> keywordSet = new HashSet< String>(Arrays.asList(keywords));
扫描仪输入=新扫描仪(文件);
int counter = 0;
while(input.hasNext())
{
String key = input.next();
if(key.length()> 0)
{
if(keywordSet.contains(key))
{
map.put(key,1) ;
counter ++;
}

if(map.containsKey(key))< - 尝试内部循环这里,失败
{
int value = map.get(key) ;
value ++;
map.put(key,value);
}

}

这段代码应该是将关键字添加到密钥,并在每次发生相同的密钥时增加该值。到目前为止,它添加了关键字,但无法正确递增该值。这里是一个示例输出:

  {assert = 2,class = 2,continue = 2,default = 2,else = 2 ,...} 

基本上它会增加地图中的每个值,而不是它应该的值。我不知道我是否过分思考这个或什么。我已经尝试了一个内部循环,它给了我疯狂的结果。我真的希望我只是过分思考这个。任何帮助都非常感谢!

解决方案

对于您扫描的每个键,在地图中创建一个新条目(覆盖现有的) 。然后,下一个条件成立,所以你将计数增加1,达到值2。



内部部分应该是这样的:

  if(keywordSet.contains(key))
{
整数值= map.get(key);
if(value == null)
value = 0;
value ++;
map.put(key,value);
}

无论如何,请考虑使用某种可变整数来提高效率。您不必重写地图中的条目,您不会做太多的整数拳击操作。


I'm writing a project that captures Java keywords from a .java file and keeps track of the occurrences with a map. I've used a similar method in the past successfully, but I can't seem to adopt this method for my intended use here.

    Map<String,Integer> map = new TreeMap<String,Integer>();
    Set<String> keywordSet = new HashSet<String>(Arrays.asList(keywords));
    Scanner input = new Scanner(file);
    int counter = 0;
    while (input.hasNext())
    {
        String key = input.next();
        if (key.length() > 0)
        {
            if (keywordSet.contains(key))
            {
                map.put(key, 1);
                counter++;
            }

                if(map.containsKey(key)) <--tried inner loop here, failed
                {
                    int value = map.get(key);
                    value++;
                    map.put(key, value);
                }

        }

This block of code is supposed to add the keyword to the key, and increment the value each time the same key occurs. So far, it adds the keywords, but fails to properly increment the value. here is a sample output:

{assert=2, class=2, continue=2, default=2, else=2, ...} 

Basically it increments every value in the map instead of the ones it's supposed to. I'm not sure if I'm over-thinking this or what. I've tried an inner loop and it gave me insane results. I really hope I'm just over-thinking this. Any help is greatly appreciated!

解决方案

For every key you scan you create a new entry in the map (overriding the existing one). Then, the next condition holds so you increment the count by 1, reaching the value 2.

The inner part should be something like:

        if (keywordSet.contains(key))
        {
            Integer value = map.get(key);
            if (value == null)
                value = 0;
            value++;
            map.put(key, value);
        }

Anyway, consider using some kind of a mutable integer to make this more efficient. You won't have to override entries in the map, and you won't be doing too much Integer boxing operations.

这篇关于在Java中映射中计算密钥的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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