Map containsKey() method in Java

Java java.util.Map.containsKey() method is used to check a key exits or not in Map. Java Map containsKey Method returns true if this java map contains a mapping for the given key .

Syntax of Map containsKey() method

public boolean containsKey(Object key) {

}

Parameters – key key whose presence in this map is to be tested
Return Value – true if this map contains a mapping for the given key

Example 1 : Java Map containsKey method -Value present

In this example we will initialize TreeMap using put() method . Then We will check Map contains key or not using map.containskey() meethod . Because there vogue and welcomes keys are present in Map , so map containsKey() method will return true  .

import java.util.Map;
import java.util.TreeMap;

public class MapcontainsKey {
public static void main(String[] args) {
		
    	Map<String,Integer> treeMap = new TreeMap<String,Integer>();

    	treeMap.put("java",1);
    	treeMap.put("vogue",2);
    	treeMap.put("welcomes",3);
	treeMap.put("you",4);

     System.out.println("is key 'vogue' present  = "+treeMap.containsKey("vogue"));
   System.out.println("is key 'welcomes' present = "+treeMap.containsKey("welcomes"));
		
	}

}

Output:

is key 'vogue' present  = true
is key 'welcomes' present = true

Example 2 : Java Map containsKey method – Key not present

Map containsKey() method will return false if key not presentin Map . In this example we will check java map contains given key , then map.containskey() method returns false .

import java.util.Map;
import java.util.TreeMap;

public class MapcontainsKey1 {
	
	public static void main(String[] args) {
		
    	Map<String,Integer> treeMap = new TreeMap<String,Integer>();

    	treeMap.put("java",1);
    	treeMap.put("vogue",2);
    	treeMap.put("welcomes",3);
	treeMap.put("you",4);

		
System.out.println("is key 'tutorial' present  = "+treeMap.containsKey("tutorial"));
System.out.println("is key 'treemap' present = "+treeMap.containsKey("treemap"));
		
	}

}

Output:

is key 'tutorial' present  = false
is key 'treemap' present = false

Example 3 : Map.containsKey() method – Mapping String to Integer keys

In this example we will check java map contains key using map containsKey() method . Now we will initialize Map have mapping String to Integer keys . We will check for keys 1 and 9 . Because key 1 is present in Map , So map containskey method return true  and For key 9 map containsKey method return false .

import java.util.Map;
import java.util.TreeMap;

public class MapcontainsKey2 {
	public static void main(String[] args) {
		
		Map<Integer,String> treeMap = new TreeMap<Integer,String>();

		treeMap.put(1,"java");
		treeMap.put(2, "vogue");
		treeMap.put(3,"welcomes");
		treeMap.put(4,"you");

		System.out.println("is key '1' present  = "+treeMap.containsKey(1));
		System.out.println("is key '2' present = "+treeMap.containsKey(9));
		
	}
}

Output:

is key '1' present  = true
is key '2' present = false

In this example we have learned how to check java map contains key  using map containsKey() method . You can learn more java TreeMap program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 1 =