简体   繁体   English

HashMap containsKey()查询的输出未返回正确的值

[英]Output from the HashMap containsKey() query not returning the correct value

The program will print the incorrect key and value based on the If statement. 程序将基于If语句打印错误的键和值。 Can someone explain why? 有人可以解释为什么吗?

Eg Key = Uncle tom + Value = 02086542222 Key = Harry + Value = 020826262 例如,键=汤姆叔叔+值= 02086542222键=哈利+值= 020826262

Query = Uncle tom 查询=汤姆叔叔

Returns = Key = Harry + Value = 00826262 返回值=键=哈利+值= 00826262

Quote from the documentation states below: 从以下文档状态引述:

"More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))" “更正式地说,当且仅当此映射包含键k的映射,使得(key == null?k == null:key.equals(k))时,返回true。”

So I was under the impression that if(Contacts.containsKey(query)) would compare the input query against the key using key.equals(k) 因此,我的印象是if(Contacts.containsKey(query))将使用key.equals(k)将输入查询与键进行比较

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");
            }
        }

    }
}

Your program iterates over every entry in the map, asking for some input ( query ) for each entry, then checks if query is a key in the map and if it is prints the currently visited entry (which is completely unrelated to query ). 您的程序遍历地图中的每个条目,为每个条目请求一些输入( query ),然后检查query是否是地图中的键,以及它是否打印当前访问的条目(与query完全无关)。

So the output seems "correct": The map does contain "Uncle Tom", so it proceeds to print the first entry ("Harry"). 因此输出看起来是“正确的”:该地图确实包含“汤姆叔叔”,因此它将继续打印第一个条目(“哈利”)。 Note that "first" is a muddy concept in a HashMap, the iteration order of entries is unspecified. 请注意,“第一个”是HashMap中的泥泞概念,条目的迭代顺序未指定。

I don't quite understand why you loop over all entries, but the line you commented out (which prints the entry matching query ) may work better: 我不太明白为什么要遍历所有条目,但是注释掉的行(会打印出匹配query的条目)可能会更好:

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

try to use the entry variable declared in your for-loop as such 尝试这样使用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");
                }
}
            }

Hope that helps. 希望能有所帮助。 Use the entry variable on the for-loop and since it's a small program, you can use a while loop on the Scanner. 在for循环上使用entry变量,由于它是一个小程序,因此可以在Scanner上使用while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM