简体   繁体   English

HashMap.containsKey 由于某种原因不起作用

[英]HashMap.containsKey not working for some reason

I wonder why the logic if (!map.containsKey("Africa")) neither go to if block (XX) nor else block (YY)我想知道为什么逻辑if (!map.containsKey("Africa"))既不转到 if 块(XX)也不转到 else 块(YY)

public class HashMapWithListTest {

public static void main (String args[]) {
    HashMap<String,List<String>> map=new HashMap<String,List<String>>();

    //to put data firs time
    String country="USA";
    //create list for cities
    List<String> cityList=new ArrayList<String>();
    //then fill list
    cityList.add("New York");
    cityList.add("Los Angeles ");
    cityList.add("Chicago");

    //lets put this data to map
    map.put(country, cityList);

    //same thind with other data
    country="Pakistan";
    cityList=new ArrayList<String>();
    cityList.add("Lahore");
    cityList.add("Karachi");
    map.put(country, cityList);

    country="Malaysia";
    cityList=new ArrayList<String>();
    cityList.add("Kuala Lumpur");
    cityList.add("Johor Baru");
    map.put(country, cityList);

    country="Indonesia";
    cityList=new ArrayList<String>();
    cityList.add("Jakarta");
    cityList.add("Bandung");
    map.put(country, cityList);

    //now lets check what is in map
    System.out.println(map);

    //to add city in USA
    //you need to get List of cities and add new one 
    map.get("USA").add("Washington");

    //to get all values from USA
    System.out.println("city in USA:");
    List<String> tmp=map.get("USA");
    for (String city:tmp)
        System.out.println(city);

    System.out.println("x------------------x");
    map.remove("USA");
    System.out.println(map);
    System.out.println("x------------------x");
    map.get("Indonesia").add("Lembang");
    System.out.println(map.get("Indonesia"));
    System.out.println("x------------------x");
    country="Indonesia";
    cityList=new ArrayList<String>();
    cityList.add("Semarang");
    cityList.add("Bali");
    map.putIfAbsent(country, cityList);

    if (!map.containsKey("Africa")) {
        System.out.println(map.get("XX"));
    } else {
        System.out.println(map.get("YY"));
    }
}

} }

map.containsKey("Africa") returns false in your code (because there is no such key in your HashMap ) and then map.get("XX") produces null value (there is no value associated with "XX" key in your HashMap ), which is printed by System.out.println . map.containsKey("Africa")在您的代码中返回false (因为您的HashMap没有这样的键)然后map.get("XX")产生null值(在您的代码中没有与"XX"键关联的值) HashMap ),由System.out.println打印。

Code is correct, possibly there is a semantics issue代码正确,可能存在语义问题

Best way to check existence of a key:检查密钥是否存在的最佳方法:

Myobject value = map.get(key);
if (value != null) {
...
} else {
if (map.containsKey(key)) {
   // key exists but it's null
} else {
   // No such key
}
}

Because this is the way HashMap.containsKey works:因为这是 HashMap.containsKey 的工作方式:

@Override
public boolean containsKey(Object key) {
Entry<K, V> m = getEntry(key);
return m != null;
}

it is going in if loop..if you want to check, repace System.out.println(map.get("XX"));它进入 if 循环..如果你想检查,repac System.out.println(map.get("XX")); with System.out.println("XX");System.out.println("XX");

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

相关问题 HashMap.containsKey()返回false。 为什么? - HashMap.containsKey() returns false. Why? HashMap.containsKey() - 如何搜索 class? - HashMap.containsKey() - how to search for a class? java中HashMap.containsKey()的时间复杂度是多少? - What is the time complexity of HashMap.containsKey() in java? Java HashMap.containsKey()不调用equals() - Java HashMap.containsKey() doesn't call equals() Java-HashMap.containsKey返回对象的错误值 - Java - HashMap.containsKey returning incorrect values for Objects ArrayList.contains()vs HashMap.containsKey()vs HashMap.get() - ArrayList.contains() vs HashMap.containsKey() vs HashMap.get() HashMap.containsKey(key) 找不到键,自定义类用作键类型 - HashMap.containsKey(key) failing to find key, with custom class used as key type 为什么使用Hashmap.containsKey比Arrays.binarySearch运行得快得多? - Why using Hashmap.containsKey run faster considerably than Arrays.binarySearch? 文件的第一个单词没有被 hashmap.containskey 识别为与文件中稍后出现的其他单词相同的单词 - First word of file isn't being recognized as same word as other occurences later in file by hashmap.containskey 为什么在部署的环境中,对Hashmap.containsKey()的调用比本地调用慢100倍? - Why would a call to Hashmap.containsKey() be 100 times slower in deployed environment vs local?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM