- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Check if a particular value exists in Java TreeSet
The contains() method is used to check if a particular value exists.
First, create a TreeSet and add elements −
TreeSet<String> set = new TreeSet<String>(); set.add("34"); set.add("12"); set.add("67"); set.add("54"); set.add("76"); set.add("49");
Use the contains() method to check if a value exist or not i.e. 12 in this case −
set.contains("12")
The following is an example to check whether a particular value exist or not −
Example
import java.util.*; public class Demo { public static void main(String args[]){ TreeSet<String> set = new TreeSet<String>(); set.add("34"); set.add("12"); set.add("67"); set.add("54"); set.add("76"); set.add("49"); System.out.println("TreeSet elements..."); Iterator<String> i = set.iterator(); while(i.hasNext()) { System.out.println(i.next()); } System.out.println("Does element 12 exist in the TreeSet ? " + set.contains("12")); } }
Output
TreeSet elements... 12 34 49 54 67 76 Does element 12 exist in the TreeSet ? true
- Related Articles
- Check if a particular value exists in Java LinkedHashMap
- Java Program to check if a particular value exists in TreeMap
- Check if a particular key exists in Java LinkedHashMap
- Check if a particular element exists in Java LinkedHashSet
- Java Program to check if a particular element exists in HashSet
- Java Program to check if a particular key exists in TreeMap
- Java Program to check if a given value exists in a HashMap
- 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?
- Check if a value exists in a column in a MySQL table?
- Check if value exists in a comma separated list in MySQL?
- C# Program to Check if Value exists in Hashtable
- Check if a PHP cookie exists and if not set its value
- Check if a value exists in an array and get the next value JavaScript

Advertisements