简体   繁体   English

将csv文件读入HashMap <String, ArrayList<Integer> &gt;

[英]Reading a csv file into a HashMap<String, ArrayList<Integer>>

I've been trying to make a java program in which a tab delimited csv file is read line by line and the first column (which is a string) is added as a key to a hash map and the second column (integer) is it's value. 我一直在尝试制作一个Java程序,其中逐行读取制表符分隔的csv文件,并将第一列(是字符串)添加为哈希映射的键,第二列(整数)是值。

In the input file, there are duplicate keys but with different values so I was going to add the value to the existing key to form an ArrayList of values. 在输入文件中,有重复的键,但是具有不同的值,因此我打算将值添加到现有键中以形成值的ArrayList。

I can't figure out the best way of doing this and was wondering if anyone could help? 我不知道这样做的最佳方法,并且想知道是否有人可以提供帮助?

Thanks 谢谢

EDIT: sorry guys, heres where i've got to with the code so far: I should add the first column is the value and the second column is the key. 编辑:抱歉,到目前为止,这里是我必须处理的代码:我应该添加第一列为值,第二列为键。

public class WordNet {

    private final HashMap<String, ArrayList<Integer>> words;
    private final static String LEXICAL_UNITS_FILE = "wordnet_data/wn_s.csv";

    public WordNet() throws FileNotFoundException, IOException {

        words = new HashMap<>();
        readLexicalUnitsFile();
    }

    private void readLexicalUnitsFile() throws FileNotFoundException, IOException{

        BufferedReader in = new BufferedReader(new FileReader(LEXICAL_UNITS_FILE));
        String line;

        while ((line = in.readLine()) != null) {
            String columns[] = line.split("\t");
            if (!words.containsKey(columns[1])) {
                words.put(columns[1], new ArrayList<>());
            }

        }
        in.close();

    }

You are close 你近了

String columns[] = line.split("\t");
if (!words.containsKey(columns[1])) {
    words.put(columns[1], new ArrayList<>());
}

should be 应该

String columns[] = line.split("\t");
String key = columns[0];                // enhance readability of code below
List<Integer> list = words.get(key);    // try to fetch the list
if (list == null)                       // check if the key is defined
{                                       //   if not
    list = new ArrayList<>();           //      create a new list
    words.put(key,list);                //      and add it to the map
}
list.add(new Integer(columns[1]));      // in either case, add the value to the list

In response to the OP's comment/question 回应OP的评论/问题

... the final line just adds the integer to the list but not to the hashmap, does something need to be added after that? ...最后一行只是将整数添加到列表中,而不是哈希表中,此后是否需要添加一些内容?

After the statement 声明后

List<Integer> list = words.get(key);

there are two possibilities. 有两种可能性。 If list is non-null, then it is a reference to (not a copy of) the list that is already in the map. 如果list为非null,则它是对地图中已经存在的列表的引用 (而不是其副本)。

If list is null , then we know the map does not contain the given key. 如果listnull ,则我们知道映射不包含给定的键。 In that case we create a new empty list, set the variable list as a reference to the newly created list, and then add the list to the map for the key. 在这种情况下,我们将创建一个新的空列表,将变量list设置为对新创建列表的引用,然后将该列表添加到键的映射中。

In either case, when we reach 无论哪种情况,当我们到达

list.add(new Integer(columns[1]));

the variable list contains a reference to an ArrayList that is already in the map, either the one that was there before, or one we just creatd and added. 变量list包含对映射中已经存在的ArrayList的引用,该引用既可以是以前存在的,也可以是我们刚刚创建并添加的。 We just add the value to it. 我们只添加价值。

I should add the first column is the value and the second column is the key. 我应该添加第一列为值,第二列为键。

You could remplace the ArrayList declaration by a List declaration. 您可以通过List声明替换ArrayList声明。 But it is not very problematic. 但这不是很成问题。

Anyway, not tested but the logic should be such as : 无论如何,未经测试,但逻辑应为:

    while ((line = in.readLine()) != null) {
      String columns[] = line.split("\t");
      ArrayList<Integer> valueForCurrentLine = words.get(columns[1]);

      // you instantiate and put the arrayList once
      if (valueForCurrentLine==null){
          valueForCurrentLine = new  ArrayList<Integer>();
          words.put(columns[1],valueForCurrentLine);
      }

      valueForCurrentLine.add(columns[0]);

Upvote to Jim Garrison's answer above. 支持上述吉姆·加里森的答案。 Here's a little more... (Yes, you should check/mark his answer as the one that solved it) 还有更多...(是的,您应该检查/标记他的答案为解决问题的答案)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordNet {

    private final Map<String, List<Integer>> words;
    private final static String LEXICAL_UNITS_FILE = "src/net/bwillard/practice/code/wn_s.csv";

    /**
     * 
     * @throws FileNotFoundException
     * @throws IOException
     */
    public WordNet() throws FileNotFoundException, IOException {
        words = new HashMap<>();
        readLexicalUnitsFile();
    }

    /**
     * 
     * @throws FileNotFoundException
     * @throws IOException
     */
    private void readLexicalUnitsFile() throws FileNotFoundException, IOException {

        BufferedReader in = new BufferedReader(new FileReader(LEXICAL_UNITS_FILE));
        String line;

        while ((line = in.readLine()) != null) {
            String columns[] = line.split("\t");
            String key = columns[0];
            int valueInt;
            List<Integer> valueList;

            try {
                valueInt = Integer.parseInt(columns[1]);
            } catch (NumberFormatException e) {
                System.out.println(e);
                continue;
            }

            if (words.containsKey(key)) {
                valueList = words.get(key);
            } else {
                valueList = new ArrayList<>();
                words.put(key, valueList);
            }

            valueList.add(valueInt);
        }

        in.close();
    }

    //You can test this file by running it as a standalone app....
    public static void main(String[] args) {
        try {
            WordNet wn = new WordNet();
            for (String k : wn.words.keySet()) {
                System.out.println(k + " " + wn.words.get(k));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM