来自 CSV 的 LinkedHashMap 未获取所有条目
LinkedHashMap from CSV not getting all entries
CSV文件按顺序转换成变量。我一直在尝试读取一个包含两列的 CSV 文件,一个是标题,然后是一个条目列表。
目前我一直在使用LinkedHashMap;使用以下循环读取、拆分和创建 LinkedHashMap。
但是它目前卡在我的 CSV 的第 5 行。这是当前的读取循环:
public static LinkedHashMap<String, ArrayList<String>> runningOrderMap(String filename) throws IOException {
LinkedHashMap<String, ArrayList<String>> linkedHashMap = new LinkedHashMap<>(50);
String currentLine = ""; //init iterator variable
String[] valuesTMP;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while((currentLine = bufferedReader.readLine()) != null){
valuesTMP = currentLine.split(", ");
ArrayList<String> values = new ArrayList<>();
String key = valuesTMP[0].split("\t")[0].trim();
values.add(valuesTMP[0].split("\t")[1].trim());
for(int i = 1; i < valuesTMP.length; i++){
values.add(valuesTMP[i]);
System.out.println(valuesTMP[i]);
linkedHashMap.put(key, values);
}
}
System.out.println("linked hashmap:"+linkedHashMap.keySet().size());
return linkedHashMap;
}
示例数据的格式如下,标题的长度各不相同,一个选项卡,然后是这样的内容条目列表:
title content, content2
title example content, content2, content3
title example three content, content2
title example content, content2
这个数据持续了大约 20 行,但是 LinkedHashMap 不会超过第 5 行:
title example two content
我需要保留数组中的行顺序。
看来我知道错了)
尝试将行 linkedHashMap.put(key, values);
移出内部 for
循环,如下所示:
public static LinkedHashMap<String, ArrayList<String>> runningOrderMap(String filename) throws IOException {
LinkedHashMap<String, ArrayList<String>> linkedHashMap = new LinkedHashMap<>(50);
String currentLine = ""; //init iterator variable
String[] valuesTMP;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while((currentLine = bufferedReader.readLine()) != null){
valuesTMP = currentLine.split(", ");
ArrayList<String> values = new ArrayList<>();
String key = valuesTMP[0].split("\t")[0].trim();
values.add(valuesTMP[0].split("\t")[1].trim());
for(int i = 1; i < valuesTMP.length; i++){
values.add(valuesTMP[i]);
System.out.println(valuesTMP[i]);
}
linkedHashMap.put(key, values); // <--this line was moved out from internal for loop
}
System.out.println("linked hashmap:"+linkedHashMap.keySet().size());
return linkedHashMap;
}
因为,你看,这个内部for
循环只有在content
有多个部分时才会执行
CSV文件按顺序转换成变量。我一直在尝试读取一个包含两列的 CSV 文件,一个是标题,然后是一个条目列表。
目前我一直在使用LinkedHashMap;使用以下循环读取、拆分和创建 LinkedHashMap。
但是它目前卡在我的 CSV 的第 5 行。这是当前的读取循环:
public static LinkedHashMap<String, ArrayList<String>> runningOrderMap(String filename) throws IOException {
LinkedHashMap<String, ArrayList<String>> linkedHashMap = new LinkedHashMap<>(50);
String currentLine = ""; //init iterator variable
String[] valuesTMP;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while((currentLine = bufferedReader.readLine()) != null){
valuesTMP = currentLine.split(", ");
ArrayList<String> values = new ArrayList<>();
String key = valuesTMP[0].split("\t")[0].trim();
values.add(valuesTMP[0].split("\t")[1].trim());
for(int i = 1; i < valuesTMP.length; i++){
values.add(valuesTMP[i]);
System.out.println(valuesTMP[i]);
linkedHashMap.put(key, values);
}
}
System.out.println("linked hashmap:"+linkedHashMap.keySet().size());
return linkedHashMap;
}
示例数据的格式如下,标题的长度各不相同,一个选项卡,然后是这样的内容条目列表:
title content, content2
title example content, content2, content3
title example three content, content2
title example content, content2
这个数据持续了大约 20 行,但是 LinkedHashMap 不会超过第 5 行:
title example two content
我需要保留数组中的行顺序。
看来我知道错了)
尝试将行 linkedHashMap.put(key, values);
移出内部 for
循环,如下所示:
public static LinkedHashMap<String, ArrayList<String>> runningOrderMap(String filename) throws IOException {
LinkedHashMap<String, ArrayList<String>> linkedHashMap = new LinkedHashMap<>(50);
String currentLine = ""; //init iterator variable
String[] valuesTMP;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while((currentLine = bufferedReader.readLine()) != null){
valuesTMP = currentLine.split(", ");
ArrayList<String> values = new ArrayList<>();
String key = valuesTMP[0].split("\t")[0].trim();
values.add(valuesTMP[0].split("\t")[1].trim());
for(int i = 1; i < valuesTMP.length; i++){
values.add(valuesTMP[i]);
System.out.println(valuesTMP[i]);
}
linkedHashMap.put(key, values); // <--this line was moved out from internal for loop
}
System.out.println("linked hashmap:"+linkedHashMap.keySet().size());
return linkedHashMap;
}
因为,你看,这个内部for
循环只有在content