TreeSet contains() Method in Java With Examples Last Updated : 26 Jan, 2022 Comments Improve Suggest changes Like Article Like Report TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface. This class provides numerous methods of which let us discuss contains() method of TreeSet class present inside java.util package is used to check if a specific element is present in the TreeSet or not. So basically it is used to check if a TreeSet contains any particular element. Syntax: Tree_Set.contains(Object element) Parameters: The type of TreeSet. This is the element that needs to be checked if it is present in the TreeSet or not. Return Value: A boolean value, true if the element is present in the set else it returns false. Exceptions: It throws two types of exceptions listed below as follows: NullPointerException: If the specified element is nullClassCastException: If the specified element cannot be compared with the elements that currently exist in the set.Tip: As we do know treeSet uses natural ordering and its comparator does not permit the null elements hence forth NullPointerException arises into play. Example Java // Java Program to Illustrate contains() method // of TreeSet class // Importing required classes import java.io.*; import java.util.TreeSet; // Main class public class GFG { // Main driver method public static void main(String args[]) { // Creating an empty TreeSet of string type TreeSet<String> tree = new TreeSet<String>(); // Adding elements in TreeSet // Using add() method to tree.add("Welcome"); tree.add("To"); tree.add("Geeks"); tree.add("4"); tree.add("Geeks"); tree.add("TreeSet"); // Displaying the TreeSet System.out.println("TreeSet: " + tree); // Use-case 1 // Check for specific element in the above TreeSet // object using contains() method of TreeSet class // Printing a boolean value System.out.println( "Does the Set contains 'TreeSet'? " + tree.contains("TreeSet")); // Use-case 2 // Check for specific element in the above TreeSet // object Say custom element be "4" System.out.println("Does the Set contains '4'? " + tree.contains("4")); // Use-case 3 // Check if the list contains "No" System.out.println("Does the Set contains 'No'? " + tree.contains("No")); } } Output: TreeSet: [4, Geeks, To, TreeSet, Welcome] Does the Set contains 'TreeSet'? true Does the Set contains '4'? true Does the Set contains 'No'? false Output Explanation: As we inserted elements in TreeSet {"Welcome", "To", "Geeks", "4", "Geeks", "TreeSet"} then we checks for a specific elements bypassing element as an argument to contains() method which we now returns boolean true if present else boolean false. The key point to remember is in java we only have true-false in boolean value nor do 0-1, take this note with you geeks. Comment More infoAdvertise with us Next Article TreeSet descendingIterator() method in Java with Examples chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-treeset +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads TreeSet in Java TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black tree) for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equ 13 min read TreeSet add() Method in Java The Java.util.TreeSet.add() method in Java TreeSet is used to add a specific element into a TreeSet. The function adds the element only if the specified element is not already present in the set else the function return False if the element is not present in the TreeSet. Syntax: Tree_Set.add(Object 1 min read TreeSet addAll() Method in Java The java.util.TreeSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any typ 2 min read TreeSet ceiling() method in Java with Examples The ceiling() method of java.util.TreeSet<E> class is used to return the least element in this set greater than or equal to the given element, or null if there is no such element. Syntax: public E ceiling(E e) Parameters: This method takes the value e as a parameter which is to be matched. Ret 2 min read TreeSet clear() Method in Java The Java.util.TreeSet.clear() method is used to remove all the elements from a TreeSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing TreeSet. Syntax: Tree_Set.clear() Pa 1 min read TreeSet clone() Method in Java The Java.util.TreeSet.clone() method is used to return a shallow copy of the mentioned tree set. It just creates a copy of the set. Syntax: Tree_Set.clone() Parameters: The method does not take any parameters. Return Value: The function just returns a copy of the TreeSet. Below program illustrates t 1 min read TreeSet contains() Method in Java With Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co 3 min read TreeSet descendingIterator() method in Java with Examples The descendingIterator() method of java.util.TreeSet<E> class is used to return an iterator over the elements in this set in descending order.Syntax: public Iterator descendingIterator()Return Value: This method returns an iterator over the elements in this set in descending order.Below are th 2 min read TreeSet descendingSet() method in Java with Examples The descendingSet() method of java.util.TreeSet<E> class is used to return a reverse order view of the elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. If either set is modified while an iterati 2 min read TreeSet first() Method in Java The Java.util.TreeSet.first() method is used to return the first of the element of a TreeSet. The first element here is being referred to the lowest of the elements in the set. If the elements are of integer types then the smallest integer is returned. If the elements are of the string types then th 3 min read Like