尝试合并2个HashMap并求和整数值时出现“')'预期错误”

时间:2019-05-06 18:41:36

标签: java merge hashmap

我正在尝试使用Java8 Merge方法合并两个哈希图。但是,Java表示我缺少多个,而且我不知道为什么不进行编译。

我尝试使用for循环对其进行修复,例如

for (Map.Entry<String, Integer> e : other_numbers.entrySet())
    numbers.merge(e.getKey(), e.getValue(), Integer::sum);

合并2个哈希图


try {
  // here is where we go into each
  Map<String, Integer> masterHash = new HashMap<>();
  for (int x = 0; x < args.length; x++) {
   String serialName = (args[x].substring(0, args[x].length() - 4) + ".hm");
   System.out.println(serialName);
   FileInputStream fos = new FileInputStream(serialName);
   ObjectInputStream ois = new ObjectInputStream(fos);
   Map hash = (HashMap) ois.readObject();
   //System.out.println(hash);
    hash.forEach((k, v) -> masterHash.merge(k, v, Integer::sum));
}

错误


PS C:\Users\Chuggy\Desktop\CPSC\threads> javac *.java

scannertest.java:68: error: ')' expected
    hash.forEach((k, v) -> masterHash.merge(k, v, Integer::sum));
                   ^
scannertest.java:68: error: illegal start of expression
    hash.forEach((k, v) -> masterHash.merge(k, v, Integer::sum));
                         ^
scannertest.java:68: error: ')' expected
    hash.forEach((k, v) -> masterHash.merge(k, v, Integer::sum));
                                                         ^
scannertest.java:68: error: illegal start of expression
    hash.forEach((k, v) -> masterHash.merge(k, v, Integer::sum));
                                                          ^
scannertest.java:68: error: ';' expected
    hash.forEach((k, v) -> masterHash.merge(k, v, Integer::sum));
                                                              ^
5 errors

0 个答案:

没有答案