使用 HashMap 计算数组中某些整数的数量

Using HashMap to count the quantity of certain integers from an array

我有一个如下所示的数组:由 14 个整数组成的值

String[] helloarray = new String[]
{"30","31","32","33","34","35","36","37","38","39","60","61","62","63"};

使用如下哈希图:

Map<String, Integer> hellocnt  = new HashMap<String,Integer>();

目标是找到文件中存在的每个整数的出现次数或总数?

// using bufferedreader to read and extract the lines
// and integers from the file and storing it in intvalue
String temp[] = line.split(" ");
for (i = 0; i < temp.length; i++) {
    String intvalue = temp[i];
}

现在我需要匹配文件中存在的每个整数的数量 times/quantity (intvalue)

int e = 0;
for (e = 0; e < helloarray.length; e++) {
    if (intvalue.contains(helloarray[e])) {
        System.out.println("found: " + helloarray[e]);
        if (helloarray[e] == "32") {
            // Should I use the hashmap here since I don't want to use
            // MULTIPLE IF BLOCKS checking for each integers and want 
            // to reuse the same map function and store the incremented 
            // counter after the completion?
        }
    }
}

请推荐一种优化的非暴力破解方法来实现它?

假设在输入文件中,您的数字由 space 分隔,例如

34 45 345 345 345 546523 423 234 23 4234 234 23412
234 345 435 456 453 643 455324 45523 657 87686 565
9393 8584 343 4324

以下代码可用于计算给定文件中的数字。

public Map<String, Integer> getNumbersCount(String fileName) throws IOException{
    Map<String, Integer> numberVsCountMap = new HashMap<>();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
    String line;
    while((line = bufferedReader.readLine()) != null ) {
        String numbers[] = line.split(" ");
        for(String number : numbers) {
            int count = numberVsCountMap.getOrDefault(number, 0);
            numberVsCountMap.put(number, count + 1);
        }
    }
    return numberVsCountMap;
}

已编辑代码(带过滤)

    public Map<String, Integer> getNumbersCount(String fileName, String [] filterArray) throws IOException {
    Map<String, Integer> numberVsCountMap = new HashMap<>();
    Set<String> filterSet = Arrays.stream(filterArray).collect(Collectors.toSet());

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
    String line;
    while((line = bufferedReader.readLine()) != null ) {
        String numbers[] = line.split(" ");
        for(String number : numbers) {
            if(filterSet.contains(number)) {
                int count = numberVsCountMap.getOrDefault(number, 0);
                numberVsCountMap.put(number, count + 1);
            }
        }
    }
    return numberVsCountMap;
}

因为 Java 8 你可以使用 merge 方法:

String[] arr = {"37", "31", "32", "33", "34", "35", "36", "36", "37", "37"};
// HashMap can be used as well
Map<String, Integer> map = new TreeMap<>();
// if the value is absent then 1, otherwise increase and update it
for (String str : arr) map.merge(str, 1, Integer::sum);
// output
System.out.println(map);
// {31=1, 32=1, 33=1, 34=1, 35=1, 36=2, 37=3}

具体做法如下

  1. 像这样设置数组和映射后:
String[] helloarray = new String[]
        {"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "60", "61", "62", "63"};
Map<String, Integer> hellocnt = new HashMap<String, Integer>();
for (String str : helloarray)
    hellocnt.put(str, 0);
  1. 对于您阅读的每一行:
String temp[] = line.split(" ");
for (i = 0; i < temp.length; i++) {
    String intvalue = temp[i];
    if (hellocnt.containsKey(intvalue)) {
        hellocnt.put(intvalue, hellocnt.getOrDefault(intvalue, 0) + 1);
    }
}
  1. 然后您只需执行以下操作即可查看地图的内容:
System.out.println(hellocnt);