How to Check If TreeMap contains a Particular Value in Java


In this example we will show how to check if TreeMap object contains a particular value with containsValue method of TreeMap class in Java.

Source Code

package com.beginner.examples;

import java.util.TreeMap;

public class CheckValueOfTreeMap {

	public static void main(String[] args) {
		
		//create TreeMap.
		TreeMap treeMap = new TreeMap();
		
		treeMap.put("1","Alice");
		treeMap.put("2","Bob");
		treeMap.put("3","Jack");
		
		/*
		 * To check whether a particular value exists in TreeMap using containsValue method of TreeMap.
		 */
		boolean result = treeMap.containsValue("Bob");
		System.out.println("Bob exists in TreeMap ? : " + result);

	}

}

Output:

Bob exists in TreeMap ? : true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments