Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用不区分大小写的比较计算arraylist中字符串的出现次数_Java_String_Arraylist_Case Insensitive - Fatal编程技术网

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

Java 使用不区分大小写的比较计算arraylist中字符串的出现次数,java,string,arraylist,case-insensitive,Java,String,Arraylist,Case Insensitive,我必须统计包含字符串和整数的数组列表中每个字符串的出现次数。现在我必须忽略对应于每个项目数量的int变量,只需计算列表中每个字符串的重复次数 我的问题是,在课堂上,我们只使用ints。现在关于管柱,我遇到了套管问题,因为“abc”不同于“abc”,而“Ghi的定义”不同于“Ghi的定义” 现在我掌握的代码是: Map<String, Integer> getCount1 = new HashMap<>(); { for (ItemsList i : list) {

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

我的问题是,在课堂上,我们只使用ints。现在关于管柱,我遇到了套管问题,因为“abc”不同于“abc”,而“Ghi的定义”不同于“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());
}
Map getCount1=newhashmap();
{
对于(项目列表一:列表){
整数计数=getCount1.get(i.name);
如果(计数=null){
计数=0;
}
getCount1.put(i.name,(count.intValue()+1));
}
对于(Map.Entry:getCount1.entrySet())
f、 println(entry.getKey()+”:“+entry.getValue());
}
但正如我所说:它没有正确地计算发生的次数。例如,在我的列表中有一个事件称为“Abc的Abc”,然后在input.txt文件列表中,我有相同的事件4次——“Abc的Abc”;“abc的abc”;“Abc中的Abc”和“Abc中的Abc”-写得都不一样,它们是分开计算的,而不是同一事件发生4次

早些时候,当我在计算总数和平均数时,我能够使用
equalsIgnoreCase()
,因此它在那里工作得很好,所以不管大小写如何,它都会将它们计算在正确的列表中,而不仅仅是一次出现几次

有没有一种方法,我可以使用忽略的情况下,或转换为所有相同的情况下,然后再计算它们

只是一个更新:我没有在那里尝试
.toLowerCase()
,而是在
文件读取器
中使用它,当它读取.txt文件时,它工作了
I.name=name.toLowerCase()

感谢您的时间和帮助

试试这个:

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);
        }
    }
}
public void getCount(){
Map countMap=新的HashMap();
用于(项目列表i:项目列表){
if(countMap.containsKey(i.Name.toLowerCase()){
get(i.Name.toLowerCase())+;
}
否则{
countMap.put(i.Name.toLowerCase(),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);
        }
    }
}
public void getCount(){
Map countMap=新的HashMap();
用于(项目列表i:项目列表){
if(countMap.containsKey(i.Name.toLowerCase()){
get(i.Name.toLowerCase())+;
}
否则{
countMap.put(i.Name.toLowerCase(),1);
}
}
}
哈希映射的值区分大小写,因此需要将字符串值大写或小写。请参见下面修改的代码:

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());
}
Map getCount1=newhashmap();
{
对于(项目列表一:列表){
整数计数=getCount1.get(i.name);
如果(计数=null){
计数=0;
}
getCount1.put(i.name.toString().toLowerCase(),(count.intValue()+1));
}
对于(Map.Entry:getCount1.entrySet())
f、 println(entry.getKey()+”:“+entry.getValue());
}
作为一个风格点,我会对项目列表中的项目使用更具描述性的名称,如
item

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

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());
}
Map getCount1=newhashmap();
{
对于(项目列表一:列表){
整数计数=getCount1.get(i.name);
如果(计数=null){
计数=0;
}
getCount1.put(i.name.toString().toLowerCase(),(count.intValue()+1));
}
对于(Map.Entry:getCount1.entrySet())
f、 println(entry.getKey()+”:“+entry.getValue());
}

作为一个风格点,我会对项目列表中的项目使用更具描述性的名称,如
item

我没有在那里尝试
.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();            
}
static void readFile(ArrayList列表)引发IOException{
BufferedReader in=新的BufferedReader(
新的文件读取器(“input.txt”)
);
字符串文本;
而((text=in.readLine())!=null){
如果(text.length()==0)中断;
扫描仪行=新扫描仪(文本);
linha.useDelimiter(“\\s*:\\s*”);
字符串名称=line.next();
int qtt=line.nextInt();
项目i=新项目();
i、 name=name.toLowerCase();
i、 qtt=qtt;
列表.添加(i);
}
in.close();
}

我没有在那里尝试
.toLowerCase()
而是在
文件读取器中使用它,当它读取.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();            
}
static void readFile(ArrayList列表)引发IOException{
BufferedReader in=新的BufferedReader(
新的文件读取器(“input.txt”)
);
字符串文本;
而((text=in.readLine())!=null){
如果(text.length()==0)中断;
扫描仪行=新扫描仪(文本);
linha.useDelimiter(“\\s*:\\s*”);
字符串名称=line.next();
int qtt=line.nextInt();
项目i=新项目();
i、 name=name.toLowerCase();
i、 qtt=qtt;
列表.添加(i);
}
in.close();
}

描述太多且不清楚。请在几行中解释需要。我需要将字符串“abc of abc”和“abc of abc”在ArrayList中作为同一事件计算两次,而不是因为Case@shaoibchikate,我认为它非常清楚。@Pureferret我能够修复它,我使用了
.toLowerCase()I.name=name.toLowerCase()并使其正常工作,感谢您的帮助