Java Program to Get the TreeSet Element By Index - GeeksforGeeks

28 Dec, 2020
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.
We have the element in the TreeSet and we want to print the Element from the TreeSet using the index. 
TreeSet<Integer> set = new TreeSet();
set.add(2);
set.add(3);
set.add(9);
set.add(5);

// TreeSet always used to get prevent
// from the duplicates in the increasing order
Set Contains -> [2,3,5,9] 
So, we have the Element in set -> [2, 3, 5, 9] .Now we cannot access the Element Directly So to access the Element we need to convert the set to array or list to access the element.
So there are many ways to get the element by index:
Converting TreeSet to array by traversing through the whole TreeSet and adding the element to array one by one.Converting TreeSet to array using .toArray() method.Converting TreeSet to ArrayList.
Converting TreeSet to array by traversing through the whole TreeSet and adding the element to array one by one.
Converting TreeSet to array using .toArray() method.
Converting TreeSet to ArrayList.
Method 1: Simply converting the TreeSet to array
We simply create an empty array. 
We traverse the given set and one by one add elements to the array
Java
// Java Program to Get the TreeSet Element By Index  import java.io.*;import java.util.*;class GFG {        public static void main (String[] args) {                TreeSet<Integer> s = new TreeSet<Integer>();         s.add(2);          s.add(3);          s.add(9);          s.add(5);                    int n = s.size();         int arr[] = new int[n];             int i = 0;                 // using for-each loop to traverse through         // the set and adding each element to array        for (int ele : s)             arr[i++] = ele;             for(int res : arr)        {            System.out.print(res+ " ");        }              System.out.println();              // getting the element at index 2      System.out.print(arr[2]);    }}
2 3 5 9 
5
Method 2: Using .toArray() Method
First converting the set to array using .toArray() method.
And accessing the Element from the Array by index.
Java
// Java Program to Get the TreeSet Element By Index  import java.io.*;import java.util.*;class GFG {    public static void main (String[] args) {                TreeSet<Integer> s = new TreeSet<Integer>();         s.add(2);          s.add(3);          s.add(9);          s.add(5);                    int n = s.size();         Integer arr[] = new Integer[n];             // .toArray() method converts the        // set s to array here        arr = s.toArray(arr);             for(int ele : arr)        {            System.out.print(ele+" ");        }                  System.out.println();                // getting the element at index 2          System.out.print(arr[2]);      }}
2 3 5 9 
5
Method 3: Converting to ArrayList
First Converting the set to list by directly using the constructor.
And then getting the Element from the List through index
Java
// Java Program to Get the TreeSet Element By Index  import java.io.*;import java.util.*;class GFG {    public static void main (String[] args) {                TreeSet<Integer> s = new TreeSet<Integer>();         s.add(2);          s.add(3);          s.add(9);          s.add(5);                    int n = s.size();                 // this constructor converts directly         // the whole TreeSet to list        List<Integer> list= new ArrayList<Integer>(s);            for(int ele : list){            System.out.print(ele+" ");        }                  System.out.println();                // getting the element at index 2          System.out.print(list.get(2));      }}
2 3 5 9 
5
java-treeset
Picked
Technical Scripter 2020
Java
JavaScript
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Generics in Java
Exceptions in Java
Convert a string to an integer in JavaScript
How to calculate the number of days between two dates in javascript?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
File uploading in React.js