合并 java 中具有相同键的两个映射并将值加在一起
Merge two maps with same keys in java and add the value together
我有这样的数据:
a 0020
b 0010
c 0030
c 0400
a 0100
因为它们是成对的,所以我使用 HashMap
来检索它们。现在我必须将它们加在一起,结果应该是一个键并将值加在一起,就像这样:
a 0120
b 0010
c 0430
我检索数据的字符串示例:
SSSSSSSSSASSSSSSSSSSSSS0020 // 它与实际数据不同,但代码是实际的。
我用 A 是键,0020 作为值
Map<String, String> col = new HashMap<>();
try {
File file = new File("file address.txt");
Scanner cmd = new Scanner(file);
String num = "";
String Name = "";
while (cmd.hasNextLine()) {
String line = cmd.nextLine();
if (line.charAt(9) == 'A') {
num = line.substring(23, 28);
Name = line.substring(29, 34);
col.put(Name, num);
}
Iterator it2 = col.entrySet().iterator();
while (it2.hasNext()) {
Entry<String, String> entry = (Entry<String, String>) it2.next();
System.out.println("name = " + entry.getKey() + " and value= " + entry.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
谢谢
在向 HashMap
添加对时,您可以检查密钥是否已经存在,如果存在,则只需将 HashMap
中的值与对中的值相加即可。
HashMap<String, Integer> map = new HashMap<>(); // your HashMap
String key; // pair key
int value; // pair value
if(map.keySet().contains(key)) {
map.put(key, map.get(key) + value);
} else {
map.put(key, value);
}
要么 col
映射中的值应该是数字类型以允许算术运算,要么应该创建另一个映射 Map<String, Integer>
来存储计算结果。
另外,读取数据时不需要嵌套循环计算总和,因为计算结果会不正确。
有几种方法可以在映射中累加每个键的总和。
- 使用方法
Map::compute
Map<String, Integer> col = new HashMap<>(); // changed value type to Integer
// ...
Integer number = 0;
while (cmd.hasNextLine()) {
String line = cmd.nextLine();
if (line.charAt(9) == 'A') {
number = Integer.valueOf(line.substring(23, 28));
Name = line.substring(29, 34);
col.compute(Name, (key, prev) -> (prev == null ? 0 : prev) + number);
}
// no need for nested loop
}
// print map contents
col.forEach((name, sum) -> System.out.print("%s %04d%n", name, sum));
- 使用方法
Map::merge
along with Integer::sum
可以用 lambda 代替 (sum, val)-> sum + val
:
Integer number = 0;
while (cmd.hasNextLine()) {
String line = cmd.nextLine();
if (line.charAt(9) == 'A') {
number = Integer.valueOf(line.substring(23, 28));
Name = line.substring(29, 34);
col.merge(Name, number, Integer::sum);
}
}
// print map contents
col.forEach((name, sum) -> System.out.print("%s %04d%n", name, sum));
我有这样的数据:
a 0020
b 0010
c 0030
c 0400
a 0100
因为它们是成对的,所以我使用 HashMap
来检索它们。现在我必须将它们加在一起,结果应该是一个键并将值加在一起,就像这样:
a 0120
b 0010
c 0430
我检索数据的字符串示例: SSSSSSSSSASSSSSSSSSSSSS0020 // 它与实际数据不同,但代码是实际的。 我用 A 是键,0020 作为值
Map<String, String> col = new HashMap<>();
try {
File file = new File("file address.txt");
Scanner cmd = new Scanner(file);
String num = "";
String Name = "";
while (cmd.hasNextLine()) {
String line = cmd.nextLine();
if (line.charAt(9) == 'A') {
num = line.substring(23, 28);
Name = line.substring(29, 34);
col.put(Name, num);
}
Iterator it2 = col.entrySet().iterator();
while (it2.hasNext()) {
Entry<String, String> entry = (Entry<String, String>) it2.next();
System.out.println("name = " + entry.getKey() + " and value= " + entry.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
谢谢
在向 HashMap
添加对时,您可以检查密钥是否已经存在,如果存在,则只需将 HashMap
中的值与对中的值相加即可。
HashMap<String, Integer> map = new HashMap<>(); // your HashMap
String key; // pair key
int value; // pair value
if(map.keySet().contains(key)) {
map.put(key, map.get(key) + value);
} else {
map.put(key, value);
}
要么 col
映射中的值应该是数字类型以允许算术运算,要么应该创建另一个映射 Map<String, Integer>
来存储计算结果。
另外,读取数据时不需要嵌套循环计算总和,因为计算结果会不正确。
有几种方法可以在映射中累加每个键的总和。
- 使用方法
Map::compute
Map<String, Integer> col = new HashMap<>(); // changed value type to Integer
// ...
Integer number = 0;
while (cmd.hasNextLine()) {
String line = cmd.nextLine();
if (line.charAt(9) == 'A') {
number = Integer.valueOf(line.substring(23, 28));
Name = line.substring(29, 34);
col.compute(Name, (key, prev) -> (prev == null ? 0 : prev) + number);
}
// no need for nested loop
}
// print map contents
col.forEach((name, sum) -> System.out.print("%s %04d%n", name, sum));
- 使用方法
Map::merge
along withInteger::sum
可以用 lambda 代替(sum, val)-> sum + val
:
Integer number = 0;
while (cmd.hasNextLine()) {
String line = cmd.nextLine();
if (line.charAt(9) == 'A') {
number = Integer.valueOf(line.substring(23, 28));
Name = line.substring(29, 34);
col.merge(Name, number, Integer::sum);
}
}
// print map contents
col.forEach((name, sum) -> System.out.print("%s %04d%n", name, sum));