代码之家  ›  专栏  ›  技术社区  ›  Isaac Manzi

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

  •  0
  • Isaac Manzi  · 技术社区  · 7 年前

    按顺序将CSV文件转换为变量。我一直在尝试读取一个CSV文件,其中包含两列,一个标题,然后是条目列表。

    然而,它目前被卡在我的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
    

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Ivan Kovbas    7 年前

    试着移动线 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;
    }
    

    仅当内容有多个部分时才执行循环

    推荐文章