[英]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
循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.