Java TreeSet

Java Treeset

What is Java TreeSet?

Java TreeSet is a class in Java that implements the Set interface, which means it is a collection of unique elements with no duplicates.

TreeSet is similar to a HashSet in that it provides constant-time performance for the basic operations (add, remove, and contains), but it has some additional features as well.

To understand the more about Java TreeSet, Read the Complete Article.

Creating a TreeSet in Java

  • Import the TreeSet class: First, you need to import the TreeSet class into your Java program
  • Create a TreeSet object: To create a TreeSet, you need to declare a variable of type TreeSet and use the new keyword to create a new instance of the TreeSet class.
  • Add elements to the TreeSet: Once you have created a TreeSet object, you can add elements to it using the add() method.
  • Access elements in the TreeSet: You can access the elements in the TreeSet using the various methods provided by the TreeSet class, such as first(), last(), and iterator().
TreeSet treeSet = new TreeSet();
Treeset in Java

Advantages of Java TreeSet: 

Methods of Java TreeSet

MethodsDescription
add(E e): Adds the specified element to the set if it is not already present.
clear(): Removes all of the elements from the set.

first():

Returns the first (lowest) element currently in the set.

remove(Object o):Removes the specified element from the set if it is present.
iterator(): Returns an iterator over the elements in the set, in ascending order.
last():Returns the last (highest) element currently in the set.

These are some of the commonly used methods in the TreeSet class. There are other methods available in the TreeSet class as well, such as clone(), isEmpty(), toArray(), and others.

Example 1: Java TreeSet Program 

Run

import java.util.*;

 
public class Main
{
  
public static void main (String[]args)
  {
    
      // Create a TreeSet of Strings
      Set < String > set = new TreeSet <> ();
    
 
      // Add some elements to the set
      set.add ("apple");
    
set.add ("banana");
    
set.add ("orange");
    
set.add ("pear");
    
 
      // Print the set
      System.out.println (set);
    
 
      // Check if an element is in the set
      boolean containsBanana = set.contains ("banana");
    
System.out.println ("Contains banana? " + containsBanana);
    
 
      // Remove an element from the set
      set.remove ("orange");
    
 
      // Print the set again
      System.out.println (set);

} 
} 

Output

[apple, banana, orange, pear]
Contains banana? true
[apple, banana, pear]

Example 2: Java Try with resources 

Run
import java.util.TreeSet;


public class Main
{

  public static void main (String[]args)
  {

    // Create a TreeSet
    TreeSet < String > treeSet = new TreeSet <> ();


    // Add elements to the TreeSet
    treeSet.add ("Apple");

    treeSet.add ("Banana");

    treeSet.add ("Cherry");


    // Print the TreeSet
    System.out.println ("TreeSet: " + treeSet);


    // Check if an element is in the TreeSet
    System.out.println ("Is Banana in the TreeSet? " +
			treeSet.contains ("Banana"));


    // Get the first and last elements in the TreeSet
    System.out.println ("First element: " + treeSet.first ());

    System.out.println ("Last element: " + treeSet.last ());


    // Remove an element from the TreeSet
    treeSet.remove ("Cherry");


    // Print the TreeSet again
    System.out.println ("TreeSet after removing Cherry: " + treeSet);


    // Get the size of the TreeSet
    System.out.println ("Size of TreeSet: " + treeSet.size ());


    // Clear the TreeSet
    treeSet.clear ();


    // Print the TreeSet after clearing it
    System.out.println ("TreeSet after clearing: " + treeSet);

  }
}

Output

TreeSet: [Apple, Banana, Cherry]
Is Banana in the TreeSet? true
First element: Apple
Last element: Cherry
TreeSet after removing Cherry: [Apple, Banana]
Size of TreeSet: 2
TreeSet after clearing: []

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription