- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to check if a particular value exists in TreeMap
To check if a particular value exists in TreeMap, use the containsValue() method.
Create a TreeMap first and add some elements −
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
Now, let’s say we need to check that value “Java” exists or not. For that, use the containsValue() method like this −
m.containsValue("Java")
The following is an example to check if a particular value exists in TreeMap −
Example
import java.util.*; public class Demo { public static void main(String args[]) { TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS"); System.out.println("TreeMap Elements...
"+m); System.out.println("Does value PHP exist in the TreeMap = "+m.containsValue("Java")); } }
Output
TreeMap Elements... {1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS} Does value PHP exist in the TreeMap = true
- Related Articles
- Java Program to check if a particular key exists in TreeMap
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- Java Program to check if a particular element exists in HashSet
- Check if a particular key exists in Java LinkedHashMap
- Check if a particular element exists in Java LinkedHashSet
- Java Program to check if a given value exists in a HashMap
- C# Program to Check if Value exists in Hashtable
- Check if a file exists in Java\n
- Check if a given key exists in Java HashMap
- Check if value exists for a field in a MongoDB document?
- How to check if a file exists or not in Java?
- Check if a value exists in a column in a MySQL table?
- Check if value exists in a comma separated list in MySQL?
- How to check if a Perl array contains a particular value?

Advertisements