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