【问题标题】:Counting occurrences of string in an arraylist using case-insensitive comparison使用不区分大小写的比较计算数组列表中字符串的出现次数
【发布时间】:2014-01-20 12:36:27
【问题描述】:

我必须在包含字符串和整数的ArrayList 中计算每个String 的出现次数。现在我必须忽略与每个项目的数量相对应的int 变量,只计算该列表中每个字符串的重复次数。

我的问题是,在课堂上我们只对整数进行了此操作。现在使用字符串我遇到了大小写问题,因为“abc”与“Abc”不同,“def of Ghi”与“Def of ghi”不同。

现在我的代码是这样的:

Map<String, Integer> getCount1 = new HashMap<>();
{
    for (ItemsList i : list) {
        Integer count = getCount1.get(i.name);
        if (count == null) {
            count = 0;
        }
        getCount1.put(i.name, (count.intValue() + 1));
    }

    for (Map.Entry<String, Integer> entry : getCount1.entrySet())
        f.println(entry.getKey() + " : " + entry.getValue());
}

但正如我所说:它没有正确计算出现次数。例如,我的列表中有一个名为“Abc of abc”的出现,然后在 input.txt 文件列表中我有 4 次相同的出现 -“Abc of abc”; "abc 的 abc"; "Abc of Abc" 和 "abc Of abc" - 写法不同,它分别计算它们而不是相同的出现 4 次。

在我处理总数和平均值的早期,我可以使用equalsIgnoreCase(),所以它在那里工作得很好,所以不管大小写如何,它都会在正确的列表中计算它们,但不仅仅是一次出现多次.

有没有一种方法可以在计算之前使用忽略大小写或将所有内容转换为相同大小写?

只是一个更新:我没有尝试.toLowerCase(),而是在FileReader 中使用它读取.txt 文件并且它工作i.name = name.toLowerCase();

感谢您的宝贵时间和帮助

【问题讨论】:

  • 描述性太多,不清楚。请用几行说明需要。
  • 我需要将字符串“abc of abc”和“Abc of abc”计算为 ArrayList 中的相同出现 2 次,而不是因为大小写不同而出现的不同
  • @shaoibchikate,我觉得很清楚。
  • @Pureferret 我能够修复它,当它打开 .txt 文件时,我在 FileReader 函数中使用了.toLowerCase();,它工作了我只是将它添加到 ArrayList i.name = name.toLowerCase(); 的字符串行并得到它的工作,感谢您的时间和帮助,我非常感谢它
  • @Pureferret 我用它更新了我的帖子,但我肯定也会添加答案

标签: java string arraylist case-insensitive


【解决方案1】:

试试这个:

public void getCount(){
    Map<String, Integer> countMap = new HashMap<String, Integer>();
    for(ItemsList i : itemsList){
        if(countMap.containsKey(i.Name.toLowerCase())){
            countMap.get(i.Name.toLowerCase())++;
        }
        else{
            countMap.put(i.Name.toLowerCase(),1);
        }
    }
}

【讨论】:

  • 基本上是的,但我相信方法名称是toLowerCase
  • 我试过这个,它给了我一个错误“countMap.get(i.Name.toLowerCase())++;”行,说它需要一个变量并找到一个值。感谢您的宝贵时间,非常感谢
【解决方案2】:

HashMap 的hashing function 区分大小写,因此您需要将字符串值大写或小写。请参阅下面的修改代码:

Map<String, Integer> getCount1 = new HashMap<>();
{
    for (ItemsList i : list) {
        Integer count = getCount1.get(i.name);
        if (count == null) {
            count = 0;
        }
        getCount1.put(i.name.toString(). toLowerCase() , (count.intValue() + 1));
    }

    for (Map.Entry<String, Integer> entry : getCount1.entrySet())
        f.println(entry.getKey() + " : " + entry.getValue());
}

作为一种风格,我会为您的 ItemsList 中的项目使用更具描述性的名称,例如 item

【讨论】:

    【解决方案3】:

    我没有尝试.toLowerCase(),而是在FileReader 中使用它读取.txt 文件并且它工作i.name = name.toLowerCase();

    所以最后我的代码是这样的:

    static void readFile(ArrayList<Items> list) throws IOException {
        BufferedReader in = new BufferedReader(
            new FileReader("input.txt")
        );
        String text;
        while( (text = in.readLine()) != null ) {
            if(text.length()==0) break;
            Scanner line = new Scanner(text);
            linha.useDelimiter("\\s*:\\s*");
            String name = line.next();
            int qtt = line.nextInt();
            Items i = new Items();
            i.name = name.toLowerCase();
            i.qtt = qtt;
            list.add(i);
        }
        in.close();            
    }
    

    【讨论】:

    • 现在您可以点击绿色对勾来接受它作为答案。 (这就是 Stackexchange/stackoverflow 的工作原理)
    • @Pureferret 是的,我试过了,它说我需要等待两天才能接受我自己的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2011-05-27
    • 2021-05-09
    • 2015-07-23
    相关资源
    最近更新 更多