Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自CSV的LinkedHashMap未获取所有条目_Java_Arraylist_Hashmap_Treemap_Linkedhashmap - Fatal编程技术网

Java 来自CSV的LinkedHashMap未获取所有条目

Java 来自CSV的LinkedHashMap未获取所有条目,java,arraylist,hashmap,treemap,linkedhashmap,Java,Arraylist,Hashmap,Treemap,Linkedhashmap,按顺序将CSV文件转换为变量。我一直在尝试读取一个CSV文件,其中包含两列,一个标题,然后是条目列表 目前我一直在使用LinkedHashMap;使用以下循环读取、拆分和创建LinkedHashMap 然而,它目前被卡在我的CSV的第5行。以下是当前的读取循环: public static LinkedHashMap<String, ArrayList<String>> runningOrderMap(String filename) throws IOException

按顺序将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;
    }
该数据持续约20行,但LinkedHashMap不会超过第5行:

title example two   content

我需要保留数组中的行顺序。

似乎我知道出了什么问题)

尝试移动line
linkedHashMap.put(键,值)超出内部
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;
}
public static LinkedHashMap runningOrderMap(字符串文件名)引发IOException{
LinkedHashMap LinkedHashMap=新LinkedHashMap(50);
字符串currentLine=“;//init迭代器变量
字符串[]值stmp;
试一试{
bufferedReader=新的bufferedReader(新文件读取器(文件名));
}catch(filenotfounde异常){
e、 printStackTrace();
}
而((currentLine=bufferedReader.readLine())!=null){
valuesTMP=currentLine.split(“,”);
ArrayList值=新的ArrayList();
String key=valuesTMP[0]。拆分(“\t”)[0]。修剪();
values.add(valuesTMP[0].split(“\t”)[1].trim();
对于(int i=1;ilinkedHashMap.put(key,values);//看来我知道怎么了)

尝试将行
linkedHashMap.put(键,值);
移出内部
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;
}
public static LinkedHashMap runningOrderMap(字符串文件名)引发IOException{
LinkedHashMap LinkedHashMap=新LinkedHashMap(50);
字符串currentLine=“;//init迭代器变量
字符串[]值stmp;
试一试{
bufferedReader=新的bufferedReader(新文件读取器(文件名));
}catch(filenotfounde异常){
e、 printStackTrace();
}
而((currentLine=bufferedReader.readLine())!=null){
valuesTMP=currentLine.split(“,”);
ArrayList值=新的ArrayList();
String key=valuesTMP[0]。拆分(“\t”)[0]。修剪();
values.add(valuesTMP[0].split(“\t”)[1].trim();
对于(int i=1;ilinkedHashMap.put(键、值);//示例中的第二个和第四个条目具有相同的键
title example
-因此第二个条目将覆盖映射中的第一个条目。实际数据集只有不同的键,我无法发布实际数据,因此我使用了此项,而代码中没有跳出任何内容。可以用示例重现这种行为,但您假设它不能反映您的实际数据,因此很难为您提供更多帮助。我建议您创建一个。如果添加
System.out.println(key);
就在
linkedHashMap.put(key,values)上方,您会看到什么
?输出了正确的键,我不确定为什么只有5个键。示例中的第2个和第4个条目具有相同的键
标题示例
-因此第二个条目将覆盖地图中的第一个。真实数据集只有不同的键,我无法发布真实数据,因此我使用了此项。y中没有跳出任何内容我们的代码。可以用您的示例重现这种行为,但您说它不能反映您的实际数据,因此很难为您提供更多帮助。我建议您创建一个。如果您添加
System.out.println(key);
就在
linkedHashMap.put(key,values)上方,您会看到什么
?输出了正确的键,我不知道为什么只有5个键。非常感谢!非常感谢!