How to Create a TreeSet with a List in Java?


A TreeSet in Java stores unique elements in sorted order. It implements the SortedSet interface. The TreeSet interface internally uses a balanced tree called the Red-Black tree. A List in Java is a data structure that is used to store elements in the order in which they were added. We can create a TreeSet with a List in Java in many ways. This article deals with the ways in which a TreeSet can be created using a List in Java.

Ways to Create a TreeSet with a List in Java

There are 3 ways by which a TreeSet can be created using a List in Java. They are as follows-

  • Add elements to a TreeSet one by one from an existing list.

  • Create a TreeSet object, initialize it with a constructor, and pass the list.

  • Use the Collections.addAll() method to add elements from the List to the TreeSet.

Let us learn about these approaches in depth.

Add Elements to a TreeSet One by One from an Existing List.

This is the basic method by which we can create a TreeSet using an existing list. First, we will create a list and add elements to it. Then we will create a TreeSet object in which we will add elements from the list one by one. We will traverse through the list, and add the elements of the list to the TreeSet. In this way, a TreeSet can be formed using a List.

Example

Following is an example to create a TreeSet from a List by adding elements to the TreeSet one by one from an existing list-

import java.util.*;
public class Example{
   public static void main(String args[])
   {
      //Create a new List
      ArrayList<String> list = new ArrayList<String>();
      list.add("India");
      list.add("USA");
      list.add("France");
      list.add("India");

      //Display ArrayList
      System.out.println("The list is " + list);
 
      //Create a TreeSet
      TreeSet<String> ts= new TreeSet<String>();
      
      //add elements to the TreeSet from the list one by one
      for(String str:list){
         ts.add(str);
      }
      // Print TreeSet
      System.out.println("The TreeSet obtained from the list is " + ts);
   }
}

Output

Following is an output of the above code

The list is [India, USA, France, India]
The TreeSet obtained from the list is [France, India, USA]

Explanation

In the above code, we have created a list, added elements, and printed the same. The List can contain duplicate elements. Then we created a TreeSet. We have added elements of the list to the TreeSet by traversing the list. Here the TreeSet will contain distinct elements from the list.

Create a TreeSet Object, Initialize it with a Constructor, and Pass the List.

The next method is to create a list and add elements to it. Then we create a TreeSet object and initialize it with a constructor. Then, we pass the list as a parameter to the TreeSet constructor so that the elements in the list are added to the TreeSet.

Example

Following is an example to create a TreeSet from a List by passing the list to the TreeSet constructor-

import java.util.*;
public class Example{
   public static void main(String args[])
   {
      //Create a new List
      ArrayList<String> list = new ArrayList<String>();
      list.add("India");
      list.add("USA");
      list.add("France");
      list.add("India");

      //Display ArrayList
      System.out.println("The list is " + list);
 
      //Create a TreeSet and pass list as a parameter
      TreeSet<String> ts= new TreeSet<String>(list);
      
      // Print TreeSet
      System.out.println("The TreeSet obtained from the list is " + ts);
   }
}

Output

Following is the Output

The list is [India, USA, France, India]
The TreeSet obtained from the list is [France, India, USA]

Explanation

In the above code, we have created a list and added elements to it. A TreeSet object is then created and the list is passed as a parameter to the TreeSet constructor. As already mentioned earlier, the list can contain duplicates but the TreeSet cannot. Then we printed the list.

The addAll() method

The addAll() method of the Collections class in Java is used to add elements from one collection to the other. So, we will create a list, add elements to it and create a TreeSet. Using the addAll() method, we will add elements from the list to the TreeSet.

Example

Following is an example to create a TreeSet from a List using the addAll() method-

import java.util.*;
public class Example{
   public static void main(String args[])
   {
      //Create a new List
      ArrayList<String> list = new ArrayList<String>();
      list.add("India");
      list.add("USA");
      list.add("France");
      list.add("India");

      //Display ArrayList
      System.out.println("The list is " + list);
 
      //Create a TreeSet and pass list as a parameter
      TreeSet<String> ts= new TreeSet<String>(list);
	  
	  //addAll method
      ts.addAll(list);
      
      // Print TreeSet
      System.out.println("The TreeSet obtained from the list is " + ts);
   }
}

Output

Following is an output of the above code

The list is [India, USA, France, India]
The TreeSet obtained from the list is [France, India, USA]

Explanation

In the above code, we have created a list, added elements to it, and created a TreeSet. So, the addAll() method will add elements from the list to the TreeSet. Then, we printed the TreeSet.

Updated on: 24-Jul-2023

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements