check if map contains key java
if (map.containsKey(key)) {
// Okay, there's a key but the value is null
} else {
// Definitely no such key
}
check if map contains key java
if (map.containsKey(key)) {
// Okay, there's a key but the value is null
} else {
// Definitely no such key
}
TreeMap containsKey() method in java
import java.util.TreeMap;
public class TreeMapContainsKeyMethodExample
{
public static void main(String[] args)
{
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
// map String values to Integer keys
tm.put(56, "orange");
tm.put(62, "indigo");
tm.put(43, "red");
tm.put(91, "green");
tm.put(38, "yellow");
System.out.println("TreeMap before using containsKey() method: " + tm);
// check for key element '62'
System.out.println("Does key '62' present? " + tm.containsKey(62));
// check for key element '90'
System.out.println("Does key '90' present? " + tm.containsKey(90));
}
}
if hashmap contains only one key then how to get that key
HashMap<Node, Integer> x = qu.remove();
// This "qu" is a queue which is returning a Hashmap
// of type HashMap<Node, Integer> this hashmap contains
// only one key,value pair
Node node = x.keySet().iterator().next();
// here we are extracting that "KEY" of hashMap into variable of Node type
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us