HashMap.containsKey 由于某种原因无法正常工作
HashMap.containsKey not working for some reason
我想知道为什么逻辑 if (!map.containsKey("Africa"))
既不去 if block (XX) 也不去 else block (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
在你的代码中(因为你的 HashMap
中没有这样的键)然后 map.get("XX")
产生 null
值(没有与 HashMap
中的 "XX"
键关联的值),由 System.out.println
.
打印
代码正确,可能存在语义问题
检查密钥是否存在的最佳方法:
Myobject value = map.get(key);
if (value != null) {
...
} else {
if (map.containsKey(key)) {
// key exists but it's null
} else {
// No such key
}
}
因为这是 HashMap.containsKey 的工作方式:
@Override
public boolean containsKey(Object key) {
Entry<K, V> m = getEntry(key);
return m != null;
}
它进入 if 循环..如果你想检查,用 System.out.println("XX");
替换 System.out.println(map.get("XX"));
我想知道为什么逻辑 if (!map.containsKey("Africa"))
既不去 if block (XX) 也不去 else block (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
在你的代码中(因为你的 HashMap
中没有这样的键)然后 map.get("XX")
产生 null
值(没有与 HashMap
中的 "XX"
键关联的值),由 System.out.println
.
代码正确,可能存在语义问题
检查密钥是否存在的最佳方法:
Myobject value = map.get(key);
if (value != null) {
...
} else {
if (map.containsKey(key)) {
// key exists but it's null
} else {
// No such key
}
}
因为这是 HashMap.containsKey 的工作方式:
@Override
public boolean containsKey(Object key) {
Entry<K, V> m = getEntry(key);
return m != null;
}
它进入 if 循环..如果你想检查,用 System.out.println("XX");
System.out.println(map.get("XX"));