Java哈希表:包含键can';找不到现有密钥
想象我有一个dictionary.csv文件:Java哈希表:包含键can';找不到现有密钥,java,hashmap,hashtable,Java,Hashmap,Hashtable,想象我有一个dictionary.csv文件: "apple", "n.", "a red fruit" "exercise", "n.", "sport" "exercise", "v.", "play sport" 我已将其读入类型哈希表: 但是,content.containsKey(“苹果”)返回false。
"apple", "n.", "a red fruit"
"exercise", "n.", "sport"
"exercise", "v.", "play sport"
我已将其读入类型哈希表:
但是,content.containsKey(“苹果”)返回false。我尝试了hashmap和concurrentHashMap,但效果并不理想
下面是我的字典类代码
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
public class Dictionary {
private String filename;
private Hashtable<String, ArrayList<ArrayList<String>>> content = new Hashtable<>();
public Dictionary(String filename) throws FileNotFoundException {
// set the file name
this.filename = filename;
// read dictionary file into content
try (BufferedReader br = new BufferedReader((new FileReader(filename)))) {
String line;
// read every line
while ((line = br.readLine()) != null){
String[] values = line.split(",");
assert(values.length == 3);
// split word,
String word = values[0].toLowerCase();
ArrayList<String> meaning = new ArrayList<>();
meaning.add(values[1]);
meaning.add(values[2]);
// add word and meaning to the content
if (content.containsKey(word)){
ArrayList newMeanings = content.get(word);
newMeanings.add(meaning);
content.put(word, newMeanings);
}
else {
ArrayList<ArrayList<String>> meanings = new ArrayList<>();
meanings.add(meaning);
content.put(word, meanings);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void getMeaning(String rawWord){
String word = rawWord.toLowerCase();
if (content.containsKey(word)){
ArrayList<ArrayList<String>> meanings = content.get(word);
int numMeanings = meanings.size();
for (int i = 0; i < numMeanings; i++){
String[] meaningLst = (String[]) meanings.get(i).toArray();
System.out.println("Meaning " + (i+1) + ": " + meaningLst[0] + ". " + meaningLst[1]);
}
}
else {
System.out.println("Word not found");
}
}
}
我认为您是将
“apple”
作为键插入,而不是apple
。删除双引号。
更改:
致:
您好,问题在于输入内容应该是内容。containsKey(“\”apple\”)不是
content.containsKey(“apple”)或请删除dictionary.csv文件中的“。切勿在新代码中使用
Hashtable
。请改用Map
和HashMap
。(并将变量声明为Map
等,而不是使用特定的集合类型。)我认为您正在插入“apple”“
作为一个键,而不是apple
。删除双引号。使用现有库读取CSV,不要自己解析它,因为它会导致类似这样的问题。我支持@MarkrotVeel,并想指出数据看起来不一致,例如在“apple”
之后,您有一个:
作为分隔符,而在“exercise”
之后,您有一个=
作为分隔符。但请注意,这将删除键中的所有引号,而不仅仅是第一个和最后一个引号
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
public class Dictionary {
private String filename;
private Hashtable<String, ArrayList<ArrayList<String>>> content = new Hashtable<>();
public Dictionary(String filename) throws FileNotFoundException {
// set the file name
this.filename = filename;
// read dictionary file into content
try (BufferedReader br = new BufferedReader((new FileReader(filename)))) {
String line;
// read every line
while ((line = br.readLine()) != null){
String[] values = line.split(",");
assert(values.length == 3);
// split word,
String word = values[0].toLowerCase();
ArrayList<String> meaning = new ArrayList<>();
meaning.add(values[1]);
meaning.add(values[2]);
// add word and meaning to the content
if (content.containsKey(word)){
ArrayList newMeanings = content.get(word);
newMeanings.add(meaning);
content.put(word, newMeanings);
}
else {
ArrayList<ArrayList<String>> meanings = new ArrayList<>();
meanings.add(meaning);
content.put(word, meanings);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void getMeaning(String rawWord){
String word = rawWord.toLowerCase();
if (content.containsKey(word)){
ArrayList<ArrayList<String>> meanings = content.get(word);
int numMeanings = meanings.size();
for (int i = 0; i < numMeanings; i++){
String[] meaningLst = (String[]) meanings.get(i).toArray();
System.out.println("Meaning " + (i+1) + ": " + meaningLst[0] + ". " + meaningLst[1]);
}
}
else {
System.out.println("Word not found");
}
}
}
import java.io.FileNotFoundException;
public class Main {
public static void main(String ars[]) throws FileNotFoundException {
Dictionary dictionary = new Dictionary("dictionary.csv");
dictionary.getMeaning("apple");
}
}
String word = values[0].toLowerCase();
String word = values[0].toLowerCase();
word = word.substring(1, word.length()-1);