Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Create a TreeSet with a List in Java?

How to Create a TreeSet with a List in Java?

TreeSet is an implementation of the SortedSet interface in Java that uses a Tree for storage. TreeSet can be created from List by passing the List to the TreeSet constructor in Java or we can traverse complete List and adding each element of the List to the TreeSet. 

Example:

Input : List = [a, b, c]
Output: TreeSet = [a, b, c]

Input : List = [1, 2, 3]
Output: TreeSet = [1, 2, 3]

Approach 1:

  1. Create a List object.
  2. Enter multiple inputs in the List.
  3. Create a TreeSet Object.
  4. Initialize object with a constructor and pass List object in it.
  5. Print the Treeset.

Below is the implementation of the above approach:

Java




// Java Program to Create a TreeSet with a List
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
 
public class ExampleTreeSet {
    public static void main(String a[])
    {
        // Create new List
        List<String> fruitlist = new ArrayList<String>();
        fruitlist.add("Mango");
        fruitlist.add("Apple");
        fruitlist.add("Grape");
        fruitlist.add("Papaya");
 
        // Printing ArrayList
        System.out.println("Fruit List : " + fruitlist);
 
        // Create a TreeSet with the list
        TreeSet<String> tree_set
            = new TreeSet<String>(fruitlist);
 
        // Print TreeSet
        System.out.println("TreeSet from List : "
                           + tree_set);
    }
}


Output

Fruit List : [Mango, Apple, Grape, Papaya]
TreeSet from List : [Apple, Grape, Mango, Papaya]

Time Complexity: O(N)

Approach 2: 

  1. Create a List object.
  2. Enter multiple inputs in the List.
  3. Create a TreeSet Object.
  4. Start List traversal and add that element in the TreeSet.
  5. After complete traversal, Print the Treeset.

Below is the implementation of the above approach:

Java




// Java Program to Create a TreeSet with a List
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
 
public class ExampleTreeSet {
    public static void main(String a[])
    {
        // Create new List
        List<String> fruitlist = new ArrayList<String>();
        fruitlist.add("Mango");
        fruitlist.add("Apple");
        fruitlist.add("Grape");
        fruitlist.add("Papaya");
 
        // Printing ArrayList
        System.out.println("Fruit List : " + fruitlist);
 
        // Create a TreeSet
        TreeSet<String> tree_set = new TreeSet<String>();
 
        // Add each element in the TreeSet
        for (String i : fruitlist)
            tree_set.add(i);
 
        // Print TreeSet
        System.out.println("TreeSet from List : "
                           + tree_set);
    }
}


Output

Fruit List : [Mango, Apple, Grape, Papaya]
TreeSet from List : [Apple, Grape, Mango, Papaya]

Time Complexity: O(N)

Approach 3 : 

1. Initialize and Declare the List object with inputs. 

2. Now, Create the TreeSet object. 

3. Collections.addAll() is used to add all elements from one object to another object. 

4. Print the TreeSet.

Java




// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class GFG {
    public static void main(String[] args) {
        List<String> fruitlist = new ArrayList<String>();
        fruitlist.add("Mango");
        fruitlist.add("Apple");
        fruitlist.add("Grape");
        fruitlist.add("Papaya");
  
        // Printing ArrayList
        System.out.println("Fruit List : " + fruitlist);
  
        // Create a TreeSet
        TreeSet<String> tree_set = new TreeSet<String>();
  
        // Add all elements in the TreeSet
         
            tree_set.addAll(fruitlist);
  
        // Print TreeSet
        System.out.println("TreeSet from List : "
                           + tree_set);
     
    }
}


Output :

Fruit List : [Mango, Apple, Grape, Papaya]
TreeSet from List : [Apple, Grape, Mango,Papaya]

 

RELATED ARTICLES

Most Popular

Recent Comments