![](/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.