简体   繁体   English

使用不区分大小写的比较计算数组列表中字符串的出现次数

[英]Counting occurrences of string in an arraylist using case-insensitive comparison

I have to count the occurrences of each String in an ArrayList that includes Strings and ints. 我必须计算包含字符串和整数的ArrayList中每个String的出现次数。 Right now I have to ignore the int variable that corresponds to the quantity of every item and just count the repetitions of each String in that list. 现在,我必须忽略与每个项目的数量相对应的int变量,而只计算该列表中每个String的重复次数。

My problem is that in class we only did this with ints. 我的问题是,在课堂上我们只使用int做此操作。 Now with Strings I'm having a problem with the casing because "abc" is different than "Abc" and "def of Ghi" is different from "Def of ghi". 现在使用Strings时,我遇到了一个问题,因为“ abc”不同于“ Abc”,而“ def of Ghi”与“ Def of ghi”不同。

Right now the code I have is this: 现在我的代码是这样的:

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());
}

But as I said: it is not counting the occurrences correctly. 但是正如我说的那样:它没有正确地计算发生次数。 I have, for example, one occurrence in my list called "Abc of abc" then in the input.txt file list I have that same occurrence 4 times - "Abc of abc"; 例如,我的列表中有一个出现,称为“ abc的Abc”,然后在input.txt文件列表中,我出现了4次相同的出现-“ abc的Abc”; "abc of Abc"; “ Abc的abc”; "Abc of Abc" and "abc Of abc" - all written differently and it's counting them separately instead of the same occurrence 4 times. “ Abc of Abc”和“ abc Of abc”-均以不同的方式书写,并且分别对它们进行计数,而不是对同一事件进行4次计数。

Early on when I was working on the totals and averages I was able to use equalsIgnoreCase() so it works fine in there, so regardless of the casing, it's counting them in the right list but not as just one occurrence several times. 早些时候,当我计算总计和平均值时,我可以使用equalsIgnoreCase()使其在其中正常工作,因此无论大小写如何,它都将它们计入正确的列表中,而不是将其计为一次。

Is there a way that I could use the ignore case or convert everything to the same case before counting them? 有没有一种方法可以在忽略它们之前使用忽略大小写或将所有内容转换为相同的大小写?

Just an update : Instead of trying the .toLowerCase() there I used it in the FileReader when it reads the .txt file and it worked i.name = name.toLowerCase(); 只是一个更新 :不要试图在.toLowerCase()有,我用它在FileReader时,它读取.txt文件和它的工作i.name = name.toLowerCase();

Thanks for your time and help anyway 谢谢您的宝贵时间,无论如何都需要帮助

Try this : 尝试这个 :

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);
        }
    }
}

The hashing function for a HashMap is case sensitive, so you need to uppercase or lowercase the string values. HashMap的哈希函数区分大小写,因此您需要将字符串值大写或小写。 See below your modified code: 请参阅下面的修改后的代码:

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());
}

As a point of style I'd use a more descriptive name for the items in your ItemsList like item . 出于风格考虑,我将为ItemsList中的项目(如item使用更具描述性的名称。

Instead of trying the .toLowerCase() there I used it in the FileReader when it reads the .txt file and it worked i.name = name.toLowerCase(); 我没有尝试.toLowerCase() ,而是在FileReader使用它在读取.txt文件并且可以正常工作时使用它i.name = name.toLowerCase();

So in the end my code there was this: 所以最后我的代码是这样的:

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();            
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM