Java Program to Get the TreeSet Element By Index
Last Updated :
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.
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]);
}
}
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]);
}
}
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));
}
}
Similar Reads
TreeSet clone() Method in Java The Java.util.TreeSet.clone() method is used to return a shallow copy of the mentioned tree set. It just creates a copy of the set. Syntax: Tree_Set.clone() Parameters: The method does not take any parameters. Return Value: The function just returns a copy of the TreeSet. Below program illustrates t
1 min read
Difference Between TreeSet and SortedSet in Java TreeSet is one of the implementations of the Navigable sub-interface. It is underlying data structure is a red-black tree. The elements are stored in ascending order and more methods are available in TreeSet compare to SortedSet. We can also change the sorting parameter using a Comparator. For examp
3 min read
Find the position of an element in a Java TreeMap Given an element N and a TreeMap, the task is to find the position of this element in the given TreeMap in Java. Examples: Input: TreeMap = {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}, N = 20 Output: 2 Input: TreeMap = {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}, N = 5 Output: -1 Approach: T
2 min read
Java Program to Get the First Element from LinkedHashSet LinkedHashSet is an implementation of Set Abstract Data Type (ADT). It extends from the HashSet class which in-turn implements Set Interface. The difference between the LinkedHashSet and HashSet is the property of maintaining the element ordering. LinkedList is just a container holding sequence of e
3 min read
Convert HashSet to TreeSet in Java Hashset: Hashset in Java is generally used for operations like search, insert and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table. TreeSet: TreeSet in Java takes O(log n) for search, insert and delete which is
3 min read