【问题标题】:Output from the HashMap containsKey() query not returning the correct valueHashMap containsKey() 查询的输出未返回正确的值
【发布时间】:2019-01-27 11:25:38
【问题描述】:

程序将根据 If 语句打印不正确的键和值。 谁能解释一下原因?

例如键 = 汤姆叔叔 + 值 = 02086542222 键 = 哈利 + 值 = 020826262

查询 = 汤姆叔叔

返回 = 键 = 哈利 + 值 = 00826262

以下文档中的引述:

“更正式地说,当且仅当此映射包含键 k 的映射时才返回 true,使得 (key==null ? k==null : key.equals(k))”

所以我的印象是 if(Contacts.containsKey(query)) 会使用 key.equals(k) 将输入查询与键进行比较p>

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class HRHashMap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);

        Map<String, Integer> Contacts = new HashMap<String, Integer>();//Specify HashMap of type String

        int numOfContacts = scan.nextInt();
        scan.nextLine();

        //Add contacts
        for (int i = 0; i < numOfContacts; i++) {
            String contactName = scan.nextLine();
            int contactNumber = scan.nextInt();
            scan.nextLine();
            Contacts.put(contactName, contactNumber);
        }

        //Iterate over the Map
        for (Entry<String, Integer> entry : Contacts.entrySet()) {
            String query = scan.nextLine();
            if (Contacts.containsKey(query)) {
                //System.out.println(Contacts.get(query));
                System.out.println(entry.getKey() + "=" + entry.getValue());
            } else {
                System.out.println("Not found");
            }
        }

    }
}

【问题讨论】:

  • 您好@DeCampbell,我想您可以通过debugger 轻松发现您的问题。
  • 嗨@Ricola 我正在尝试调试,但看不出为什么返回了不正确的值。

标签: java hashmap entryset


【解决方案1】:

您的程序遍历映射中的每个条目,为每个条目请求一些输入 (query),然后检查 query 是否是映射中的键,如果它打印当前访问的条目(即与query 完全无关)。

所以输出看起来“正确”:地图确实包含“汤姆叔叔”,所以它继续打印第一个条目(“哈利”)。注意,“first”在HashMap中是一个模糊的概念,条目的迭代顺序是未指定的。

我不太明白您为什么要遍历所有条目,但您注释掉的行(打印匹配 query 的条目)可能会更好:

System.out.println(Contacts.get(query));

【讨论】:

  • 我不知道我为什么要迭代这些条目,我刚刚意识到我误解了我试图完成的任务,因此代码范围会改变。但是, System.out.println(Contacts.get(query));确实有效!您如何建议我将键与值一起打印?
  • query 是你的钥匙,对吧?所以你也可以打印出来。
  • 我以为它有一个 API,但它可以工作,谢谢!
【解决方案2】:

尝试使用在你的for循环中声明的入口变量

Scanner sc = new Scanner(System.in);

        Map<String, Integer> Contacts = new HashMap<>();

        while (sc.hasNext()) {
        String contactName = sc.nextLine();
        int contactNumber = sc.nextInt();
    sc.nextLine();
    Contacts.put(contactName, contactNumber);
    }

 for (Entry<String, Integer> entry : Contacts.entrySet()) {
while(sc.hasNext()) {
                if (entry.containsKey(sc.nextLine())) {
                    //System.out.println(Contacts.get(query));
                    System.out.println(entry.getKey() + "=" + entry.getValue());
                } else {
                    System.out.println("Not found");
                }
}
            }

希望对您有所帮助。在 for 循环中使用 entry 变量,因为它是一个小程序,你可以在 Scanner 上使用 while 循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2022-01-09
    相关资源
    最近更新 更多