简体   繁体   English

Java HashMap,get(key)方法不起作用

[英]Java HashMap, get(key) method doesnt work

I'm trying to create a PhoneBook class that uses a HashMap in Java. 我正在尝试创建一个在Java中使用HashMapPhoneBook类。 When I add an entry using the put() method in addContact() , it works fine, but when I try to retrieve values in the searchContact() method, none are returned. 当我在addContact()使用put()方法添加条目时,它可以正常工作,但是当我尝试在searchContact()方法中检索值时,不会返回任何值。 I'm not getting null values; 我没有得到空值; HashMap definitely contains the key(s) I am searching for, but the values associated with the key(s) are not being returned. HashMap肯定包含我要搜索的键,但是未返回与键相关的值。 Thank you in advance. 先感谢您。

Here is my code: 这是我的代码:

public class PhoneBookContactsImpl {

    private Map<String, List<String>> contactMap = new HashMap<String, List<String>>();

    public void addContact(String name, List<String> list) {        
        contactMap.put(name, list);
                //its working fine here
        System.out.println(contactMap.get(name));
    }

    public Map<String, List<String>> getContactMap() {

        Set set = contactMap.entrySet();
        Iterator i = contactMap.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            System.out.println(me.getKey() + " : ");
            List<String> nos = (List<String>) me.getValue();
            System.out.println("Nos = " + nos + " n ");
            System.out.println(nos.size());
        }
        return contactMap;
    }

    public List<String> searchContact(String name) throws NoDataFoundException {

        if (contactMap.isEmpty()) {
            System.out.println("Empty PhoneBook");
            throw new NoDataFoundException();
        } else {
            if (contactMap.containsValue(name))
                return contactMap.get(name);              
                                 //it doesnt retrieve valur from here
            else {
                System.out.println("No Entry for Specified Entry");
                throw new NoDataFoundException();
            }
        }
    }
}

your if statement is checking if the phonebook has name as a value, so your get is never reached. 您的if语句正在检查电话簿是否使用name作为值,因此永远不会达到您的要求。

Try this: 尝试这个:

if (contactMap.containsKey(name))
            return contactMap.get(name);    

As the other answer points out you should be checking containsKey because name is a key, not a value. 正如其他答案指出的那样,您应该检查containsKey因为name是键,而不是值。 But why not make the whole thing much easier: 但是,为什么不使整个过程变得容易得多:

public List<String> searchContact(String name) throws NoDataFoundException {
    List<String> result = contactMap.get(name);
    if (result == null) { 
        // empty map, or no matching value or value is null
        throw new NoDataFoundException();
    }
}

You are doing: 您正在执行:

if (contactMap.containsValue(name))
     return contactMap.get(name);   

and you need to do: 并且您需要执行以下操作:

if (contactMap.containsKey(name))
     return contactMap.get(name);   

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

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