Hashmaps的问题

时间:2015-04-28 20:32:09

标签: java hashmap

我在尝试迭代用户为我的hashmap中的匹配单词输入的句子时遇到问题。例如,如果用户输入“我正在与教授一起阅读”。教授是关键词。我想扫描用户为关键字教授输入的句子或它在hashmap中的同义词。到目前为止,当我运行我的程序时,代码就会挂起。

这是我的主要方法:

public static void main(String args[]) throws ParseException, IOException {
/* Initialization */
    HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();
    synonymMap= populateSynonymMap(); //populate the map
    System.out.println("Welcome To DataBase ");
    System.out.println("What would you like to know?");

    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();
    String[] inputs = input.split(" "); //Here split the sentence to words.

    for (String in : inputs) { //iterate over each word of the sentence.
        if (synonymMap.containsValue(in)) { //check if the values of synonymMap has the string in.
            for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) {
                String[] value = entry.getValue();
                if (Arrays.asList(value).contains(in)) {
                    parseFile(entry.getKey());
                    System.out.println("Have a good day!");
                    break;
                }
            }
            break;
        }
    }
}

我的hashmap方法:

private static HashMap<String, String[]> populateSynonymMap() {
    responses.put("professor", new String[]{"instructor", "teacher", "mentor"});
    responses.put("book", new String[]{"script", "text", "portfolio"});
    responses.put("office", new String[]{"room", "post", "place"});
    responses.put("day", new String[]{"time",  "date"});
    responses.put("asssignment", new String[]{"homework", "current assignment "});
    responses.put("major", new String[]{"discipline", "focus"," study"});


    return responses;
}

Parsefile检查单词的文本文件并返回句子

public static void parseFile(String s)throws FileNotFoundException {         文件文件=新文件(“data.txt”);

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        final String lineFromFile = scanner.nextLine();
        if (lineFromFile.contains(s)) {
            // a match!
            System.out.println(lineFromFile);
            // break;
        }

    }
}

2 个答案:

答案 0 :(得分:1)

问题在于这一行:if(synonymMap.containsValue(in))

由于HashMap的值是String数组类型的对象,因此您将始终返回false,因为您正在将String与String数组进行比较。

您需要做的是获取密钥然后迭代该密钥或只是调用contains方法:

for(String in : inputs){ //iterate over each word of the sentence.
    if(synonymMap.get(in) != null){ //check if the key exist


        String[] value = synonymMap.get(in);
        if (Arrays.asList(value).contains(in)) {

            parseFile(entry.getKey());


            System.out.println("Have a good day!");
            break;
        }

    break;
   }
}

答案 1 :(得分:0)

请注意代码中的输入和同义词映射的类型:

HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();
// ... more code ...
for (String in : inputs) { //iterate over each word of the sentence.

所以,这总是会返回false:

if (synonymMap.containsValue(in)) { //check if the values of synonymMap has the string in.

现在,您需要:

  

扫描用户输入的句子,用于关键字教授它在hashmap 中的同义词。

那个棘手的&#39;或者&#39;意味着您需要同时检查匹配的键和值,因为键值不在其列表值中,因为它将是多余的。所以在for (String in : inputs)内你应该有:

boolean found = false;
for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) {
    String key = entry.getKey();
    String[] value = entry.getValue();
    if (key.equals(in) || Arrays.asList(value).contains(in)) {
        found = true;
        parseFile(key);
        System.out.println("Have a good day!");
        break;
    }
}
if (found) {
    break;
}