微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java HashTable:包含键找不到现有键

如何解决Java HashTable:包含键找不到现有键

想象我有一个 dictionary.csv 文件

"apple","n.","a red fruit"
"exercise","sport"
"exercise","v.","play sport"

我已将其读入类型 Hashtable>:

content = {"apple":[["n","a red fruit"]],"exercise"=[["n.","sport"],["v.","play sport"]]}

然而, content.containsKey("apple") 返回 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");
        }
    }
}


以下是我在 Main 类中的代码

import java.io.FileNotFoundException;

public class Main {
    public static void main(String ars[]) throws FileNotFoundException {
        Dictionary dictionary = new Dictionary("dictionary.csv");
        dictionary.getMeaning("apple");
    }
}


解决方法

我认为您插入的是 "apple" 作为键而不是 apple。删除双引号。 更改:

String word = values[0].toLowerCase();

致:

String word = values[0].toLowerCase();
word = word.substring(1,word.length()-1);
,

嗨问题在于输入它应该是 content.containsKey("\"apple\"") 不是 content.containsKey("apple") 或删除 dictionary.csv 文件中的 "。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。