![](/img/trans.png)
[英]java file reading and storing it in a hashmap <String,ArrayList> and comparing with other hashMap
[英]Reading a csv file into a HashMap<String, ArrayList<Integer>>
我一直在尝试制作一个Java程序,其中逐行读取制表符分隔的csv文件,并将第一列(是字符串)添加为哈希映射的键,第二列(整数)是值。
在输入文件中,有重复的键,但是具有不同的值,因此我打算将值添加到现有键中以形成值的ArrayList。
我不知道这样做的最佳方法,并且想知道是否有人可以提供帮助?
谢谢
编辑:抱歉,到目前为止,这里是我必须处理的代码:我应该添加第一列为值,第二列为键。
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();
}
你近了
String columns[] = line.split("\t");
if (!words.containsKey(columns[1])) {
words.put(columns[1], new ArrayList<>());
}
应该
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
回应OP的评论/问题
...最后一行只是将整数添加到列表中,而不是哈希表中,此后是否需要添加一些内容?
声明后
List<Integer> list = words.get(key);
有两种可能性。 如果list
为非null,则它是对地图中已经存在的列表的引用 (而不是其副本)。
如果list
为null
,则我们知道映射不包含给定的键。 在这种情况下,我们将创建一个新的空列表,将变量list
设置为对新创建列表的引用,然后将该列表添加到键的映射中。
无论哪种情况,当我们到达
list.add(new Integer(columns[1]));
变量list
包含对映射中已经存在的ArrayList
的引用,该引用既可以是以前存在的,也可以是我们刚刚创建并添加的。 我们只添加价值。
我应该添加第一列为值,第二列为键。
您可以通过List声明替换ArrayList声明。 但这不是很成问题。
无论如何,未经测试,但逻辑应为:
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]);
支持上述吉姆·加里森的答案。 还有更多...(是的,您应该检查/标记他的答案为解决问题的答案)
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.