将CSV文件解析成HashMap存储一个空值
Parsing a CSV file into HashMap stores a null value
我正在尝试读取 csv 文件并将数据存储到哈希映射中。
我能够正确添加密钥,但是在添加值时,它会为每个值添加 null。我不确定为什么。这是我的代码:
编辑代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class ExampleActivity {
public static HashMap<String, String> hm = new HashMap<String, String>();
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("countries.csv"));
String line;
HashMap<String, String> hm = new HashMap<String, String>();
while ((line = reader.readLine()) != null) {
String str[] = line.split(",");
if (str.length > 1) {
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0].trim(), str[1]);
}
}
//System.out.println(hm);
}
public static void main(String[] args) throws IOException {
readCountry();
Scanner in = new Scanner(System.in);
String l = null;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
// System.out.println("Country Code: " + l + "\nCountry Name: " +
// hm.get(l) );
if (hm.containsKey(l)) {
System.out.println("Country Code: " + l + "\nCountry Name: "
+ hm.get(l));
} else {
System.out.println("Missing key for " + l);
}
}
}
这是 CSV 文件的示例
AFG,Afghanistan
AGO,Angola
AIA,Anguilla
...
这是输出的屏幕截图:
注释掉 hashmap 的方法局部声明,它应该可以正常工作。更改代码如下:
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("d:/countries1.csv"));
String line;
// HashMap<String, String> hm = new HashMap<String, String>(); Remove this line
试试这个:
while((line = reader.readLine()) != null){
String str[] = line.split(",");
if (str.size() > 1){
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0], str[1]);
}
}
你的 for 循环是不必要的
另请参阅黑暗骑士对空值问题的回答
编辑
您能否将其添加到您的代码中并查看它的作用:
if (hm.containsKey(l)
System.out.println("Country Code: " + l + "\nCountry Name: " + hm.get(l) );
else
System.out.println("Missing key for " + l);
System.out.println("Printing hashmap");
for(Entry<String, String> entry : hm.entrySet()) {
{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
编辑2
hm.put(str[0].trim(), str[1]);
还有下一位
Scanner in = new Scanner(System.in);
String l;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
使用 Stream API 您可以从以下片段开始。
Path path = Paths.get("countries.txt");
Map<String, String> countries = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty())
.map((Object t) -> ((String) t).split(",", 2))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l.length > 1 ? l[1] : ""));
System.out.println("countries = " + countries);
输出
countries = {AFG=Afghanistan, AIA=Anguilla, AGO=Angola}
代码段过滤掉空行,对于没有 ,
的行,值被指定为空字符串。
编辑 你修改后的 readCountry
看起来像
public static void readCountry() throws IOException {
Path path = Paths.get("countries.txt");
Map<String, String> hm = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty() && l.contains(","))
.map((Object t) -> ((String) t).split(",", 2))
.peek((String[] l) ->
System.out.printf("Data 0: %s%nData 1: %s%n", l[0], l[1]))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l[1]));
}
它将键值对存储在 hm
中并作为输出生成
Data 0: AFG
Data 1: Afghanistan
Data 0: AGO
Data 1: Angola
Data 0: AIA
Data 1: Anguilla
我正在尝试读取 csv 文件并将数据存储到哈希映射中。 我能够正确添加密钥,但是在添加值时,它会为每个值添加 null。我不确定为什么。这是我的代码:
编辑代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class ExampleActivity {
public static HashMap<String, String> hm = new HashMap<String, String>();
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("countries.csv"));
String line;
HashMap<String, String> hm = new HashMap<String, String>();
while ((line = reader.readLine()) != null) {
String str[] = line.split(",");
if (str.length > 1) {
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0].trim(), str[1]);
}
}
//System.out.println(hm);
}
public static void main(String[] args) throws IOException {
readCountry();
Scanner in = new Scanner(System.in);
String l = null;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
// System.out.println("Country Code: " + l + "\nCountry Name: " +
// hm.get(l) );
if (hm.containsKey(l)) {
System.out.println("Country Code: " + l + "\nCountry Name: "
+ hm.get(l));
} else {
System.out.println("Missing key for " + l);
}
}
}
这是 CSV 文件的示例
AFG,Afghanistan
AGO,Angola
AIA,Anguilla
...
这是输出的屏幕截图:
注释掉 hashmap 的方法局部声明,它应该可以正常工作。更改代码如下:
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("d:/countries1.csv"));
String line;
// HashMap<String, String> hm = new HashMap<String, String>(); Remove this line
试试这个:
while((line = reader.readLine()) != null){
String str[] = line.split(",");
if (str.size() > 1){
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0], str[1]);
}
}
你的 for 循环是不必要的
另请参阅黑暗骑士对空值问题的回答
编辑
您能否将其添加到您的代码中并查看它的作用:
if (hm.containsKey(l)
System.out.println("Country Code: " + l + "\nCountry Name: " + hm.get(l) );
else
System.out.println("Missing key for " + l);
System.out.println("Printing hashmap");
for(Entry<String, String> entry : hm.entrySet()) {
{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
编辑2
hm.put(str[0].trim(), str[1]);
还有下一位
Scanner in = new Scanner(System.in);
String l;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
使用 Stream API 您可以从以下片段开始。
Path path = Paths.get("countries.txt");
Map<String, String> countries = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty())
.map((Object t) -> ((String) t).split(",", 2))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l.length > 1 ? l[1] : ""));
System.out.println("countries = " + countries);
输出
countries = {AFG=Afghanistan, AIA=Anguilla, AGO=Angola}
代码段过滤掉空行,对于没有 ,
的行,值被指定为空字符串。
编辑 你修改后的 readCountry
看起来像
public static void readCountry() throws IOException {
Path path = Paths.get("countries.txt");
Map<String, String> hm = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty() && l.contains(","))
.map((Object t) -> ((String) t).split(",", 2))
.peek((String[] l) ->
System.out.printf("Data 0: %s%nData 1: %s%n", l[0], l[1]))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l[1]));
}
它将键值对存储在 hm
中并作为输出生成
Data 0: AFG
Data 1: Afghanistan
Data 0: AGO
Data 1: Angola
Data 0: AIA
Data 1: Anguilla