Map<String, Object> channelMap = new HashMap<String, Object>();
channelMap.put("1", "online");
Map<String, Object> map = new HashMap<String, Object>();
map.put("vt", 1);
if(channelMap.containsKey(map.get("vt"))) {
System.out.print("success");
} else {
System.out.print("fail");
}
输出为fail
感觉问题在于containsKey里面的值类型为Object,而channelMap的key类型是String,类型不能自动转换,所以就匹配失败。
代码改为
Map<String, Object> channelMap = new HashMap<String, Object>();
channelMap.put("1", "online");
Map<String, Object> map = new HashMap<String, Object>();
map.put("vt", 1);
if(channelMap.containsKey(map.get("vt")+"")) {
System.out.print("success");
} else {
System.out.print("fail");
}
输出为success,结果正常