Java Collection Tutorial - Java HashMap.containsKey(Object key)


HashMap.containsKey(Object key) has the following syntax.

public boolean containsKey(Object key)


In the following code shows how to use HashMap.containsKey(Object key) method.

/*w  w w  . j  av a 2s .c om*/
import java.util.HashMap;

public class Main {
   public static void main(String args[]) {
      HashMap<Integer, String> newmap = new HashMap<Integer, String>();      
      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "from");
      newmap.put(3, "");
      // check existence of key 2
      System.out.println("Check if key 2 exists: " + newmap.containsKey(2));

The code above generates the following result.