This Java Example shows how to check if TreeMap object contains a particular key using containsKey method of TreeMap class.
import java.util.TreeMap;
public class CheckParticularKeyExistTreeMapExample {
public static void main(String[] args) {
// create object of TreeMap
TreeMap phoneBook = new TreeMap();
// Adding key value pairs to TreeMap
phoneBook.put("John", "245745");
phoneBook.put("Joy", "245786");
phoneBook.put("Roy", "233783");
/**
* To check whether a particular key exists in
* TreeMap use boolean containsKey(Object key) method
* of TreeMap class. It returns true if the TreeMap
* contains mapping for specified key otherwise false.
**/
boolean blnExists = phoneBook.containsKey("Joy");
System.out.println("Name 'Joy' exists in TreeMap ? : " + blnExists);
}
}
The output is:
Name 'Joy' exists in TreeMap ? : true
No comments:
Post a Comment