How to Check If a Particular Value Exists in TreeSet in Java


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

Source Code

package com.beginner.examples;

import java.util.TreeSet;

public class CheckValueOfTreeSet {

	public static void main(String[] args) {
		
		//create TreeSet.
		TreeSet treeSet = new TreeSet();
		
		treeSet.add("A");
		treeSet.add("B");
		treeSet.add("C");
		treeSet.add("D");
		
		/*
		 * To check whether a particular value exists in TreeSet using contains method of TreeSet.
		 */
		boolean result = treeSet.contains("D");
		System.out.println("D exists in TreeSet ? : " + result);
		

	}

}

Output:

D exists in TreeSet ? : true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments