首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >来自CSV的LinkedHashMap未获取所有条目

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

Stack Overflow用户
提问于 2018-12-13 01:45:16
回答 1查看 143关注 0票数 0

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

目前,我一直在使用LinkedHashMap;使用以下循环来读取、拆分和创建LinkedHashMap。

然而,它现在卡在我的CSV的第5行。下面是当前的读取循环:

代码语言:javascript
复制
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;
    }

示例数据的格式是这样的,标题长度可变,一个制表符,然后内容条目列表如下:

代码语言:javascript
复制
title   content, content2

title example    content, content2, content3

title example three   content, content2

title example    content, content2

这个数据持续了大约20行,但是LinkedHashMap不会超过第5行:

代码语言:javascript
复制
title example two   content

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53748597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档