TreeSet Class In Java

Java TreeSet class is a part of the collection framework that implements the Set interface. The TreeSet integer values are stored in the natural sorting order, and the string values are stored according to the Dictionary values. You may also provide a custom comparator to sort the TreeSet at the time of creation to sort the elements based on the comparator.

As it implements the Set interface, null values are not allowed. Like other Collection framework classes, this class is also not synchronized, but we can explicitly make it synchronized by using it.

SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));

TreeSet Class Important Features

  • It Extends the AbstractSet class and implements a NavigableSet interface.
  • Duplicate Values and NULL values are not allowed
  • Here, the elements are stored in sorted order.
  • TreeSet does not allow the insertion of heterogeneous objects because it needs to compare the existing objects to determine the sorting order.
  • TreeSet also implements Serializable and Cloneable interfaces.
  • TreeSet class object is not synchronized or thread-safe, meaning if multiple threads act on the same class elements, the result is un-deterministic. To make it synchronized, we can use Collections.synchronizedSortedSet(new TreeSet()).
    TreeSet ts = new TreeSet();
    Set syncSet = Collections.synchronziedSet(ts);

TreeSet Class Constructors

The TreeSet has four possible constructors:

  • TreeSet t = new TreeSet(); By using this, we can create an empty TreeSet object where the objects will be stored in the default natural sorting order.
  • TreeSet t = new TreeSet(Comparator comp); This constructor is used when your requirement is sorting the elements.
  • TreeSet t = new TreeSet(Collection col); We require this type of constructor when it is necessary to convert the collection object to TreeSet Object.
  • TreeSet t = new TreeSet(SortedSet s); we use this constructor to convert the SortedSet object to TreeSet Object.

TreeSet Different Methods Used In Java With Example?

package com.java.Softwaretestingblog;
import java.util.TreeSet;
public class TreeSet_Different_Methods {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      TreeSet<String> ts = new TreeSet<String>();
        ts.add("one");
        ts.add("two");
        ts.add("three");
        System.out.println("Elements: "+ts);
        //check is set empty?
        System.out.println("Is set empty: "+ts.isEmpty());
        //delete all elements from set
        ts.clear();
        System.out.println("Is set empty: "+ts.isEmpty());
        ts.add("one");
        ts.add("two");
        ts.add("three");
        System.out.println("Size of the set: "+ts.size());
        //remove one string
        ts.remove("two");
        System.out.println("Elements: "+ts);
   }
}

Output:

Elements: [one, three, two]
Is set empty: false
Is set empty: true
Size of the set: 3
Elements: [one, three]

How do you create a treeset with a list in Java with an example?

package com.java.Softwaretestingblog;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class Create_TreeSet_List {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   List<String> li = new ArrayList<String>();
        li.add("one");
        li.add("two");
        li.add("three");
        li.add("four");
        System.out.println("List: "+li);
        //create a treeset with the list
        TreeSet<String> myset = new TreeSet<String>(li);
        System.out.println("Set: "+myset);
   }
}

Output:

List: [one, two, three, four]
Set: [four, one, three, two]

Remove Duplicate Values TreeSet Java Program: How do you remove duplicate values treeset Java with an example?

package com.java.Softwaretestingblog;
import java.util.TreeSet;
public class Remove_Duplicate_Values_TreeSet {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      String[] strArr = {"one","two","three","four","four","five"};
      TreeSet<String> unique = new TreeSet<String>();
      for(String str:strArr)
      {
         if(!unique.add(str))
         {
            System.out.println("Duplicate Entry is: "+str);
         }
      }
   }
}

Output:

Duplicate Entry is: four

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment