gpt4 book ai didi

java - 如何将重复数组元素分配为唯一键,并计算相应的整数值?

转载 作者:行者123 更新时间:2023-11-30 07:19:02 25 4
gpt4 key购买 nike

我花了一段时间研究与我类似的问题,现在我的代码被困住了。我已经实现了一个基本的 CSV 文件阅读器,但我一直在处理输入

当前结果:

Source [medium = go bio , values = 5]
Source [medium = go bio , values = 5]
Source [medium = metal sink , values = 2]
Source [medium = go bio , values = 5]
Source [medium = metal sink , values = 3]
Done

但我想要的是这样的。

输出:

[medium = go bio , values = 15]
[medium = metal sink , values = 5]

我的文本文件的内容。

go,bio,5
go,bio,5
metal,sink,2
go,bio,5
metal,sink,3

这是我的代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.util.HashMap;

public class CVSReader {
public static void main(String[] args) {
CVSReader obj = new CVSReader();
obj.run();
}

public void run() {
String csvFile = "file2.txt";
BufferedReader br = null;
String line;
String cvsSplitBy = ",";
String[] source;

try {
//HashMap here
HashMap<String, String> hash = new HashMap<>();
HashMap<String, Integer> hash2 = new HashMap<>();

br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// comma separator here
source = line.split(cvsSplitBy);

hash.put(source[0], source[0]);
hash.put(source[1], source[1]);
hash.put(source[2], source[2]);

System.out.println("Source [medium = " + hash.get(source[0]) + " " +
hash.get(source[1]) + " , values = " + hash.get(source[2]) + "]");
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}

最佳答案

首先,您需要一个Map<String, Integer>使用相同的键添加您的条目。构建 key ,解析 line 中的值,如果它已经在 Map 中将当前值添加到解析值中。然后将该值存储回 Map 。我也更喜欢 try-with-resources close 。最后,迭代Map.entrySet() 。比如,

String csvFile = "file2.txt";
String cvsSplitBy = "\\s*,\\s*"; // <-- adds support for white space(s)
// before or after the comma.
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
Map<String, Integer> hash = new HashMap<>();

String line;
while ((line = br.readLine()) != null) {
String[] source = line.split(cvsSplitBy);
String medium = source[0] + " " + source[1]; // <-- the key
int val = Integer.parseInt(source[2]); // <-- the value
if (hash.containsKey(medium)) { // <-- is the key in Map?
val += hash.get(medium); // <-- yes, add the value.
}
hash.put(medium, val); // <-- store the value back in the Map.
}
// Iterate the entry set, display the "medium" and value.
for (Map.Entry<String, Integer> entry : hash.entrySet()) {
System.out.printf("Source [medium = %s , values = %d]%n",
entry.getKey(), entry.getValue());
}
} catch (IOException e) { // <-- FileNotFoundException is a sub-class.
e.printStackTrace();
}
System.out.println("Done");

我用您提供的文本文件运行,并得到了(根据要求)

Source [medium = metal sink, values = 5]
Source [medium = go bio, values = 15]
Done

关于java - 如何将重复数组元素分配为唯一键,并计算相应的整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37904282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com