在Java中,HashMap的containsKey()方法用于判断HashMap中是否包含指定的键。具体用法如下:
HashMap<String, Integer> map = new HashMap<>();map.put("apple", 1);map.put("banana", 2);// 判断HashMap中是否包含指定的键if(map.containsKey("apple")) { System.out.println("HashMap包含键 'apple'");} else { System.out.println("HashMap不包含键 'apple'");}在上面的例子中,首先创建了一个HashMap对象,并向其中添加了两个键值对。然后使用containsKey()方法来判断HashMap中是否包含指定的键,如果包含则打印"HashMap包含键 ‘apple’“,否则打印"HashMap不包含键 ‘apple’”。


