This example shows how to get the TreeSet element by index in Java. This example also shows how to get an element from TreeSet by index using an array, list, or iterator.
How to get an element by index from Java TreeSet?
The TreeSet class in Java is a NavigableSet implementation of a TreeMap. It does not allow us to access the elements using the index. There are no direct methods to access the elements by index, but there are some workarounds using which we can do that as given below.
1. By converting to an array
The array is an index based data structure. We can convert the TreeSet object to an array and then we can access elements from it using the index as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.TreeSet; public class GetTreeSetElementsByIndexExample { public static void main(String[] args) { TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(7); tSetOddNumbers.add(3); tSetOddNumbers.add(9); tSetOddNumbers.add(5); tSetOddNumbers.add(1); System.out.println("TreeSet contains: " + tSetOddNumbers); //convert it to an array Integer[] numberArray = tSetOddNumbers.toArray( new Integer[tSetOddNumbers.size()] ); //access using array index System.out.println("Element at index 0: " + numberArray[0]); System.out.println("Element at index 4: " + numberArray[4]); } } |
Output
1 2 3 |
TreeSet contains: [1, 3, 5, 7, 9] Element at index 0: 1 Element at index 4: 9 |
As we can see from the output, the TreeSet elements are automatically sorted in the natural order which is ascending order for the Integer type.
Note: This approach requires the allocation of a new array having the TreeSet elements. If the TreeSet has a large number of elements, it may slow down the code. Plus, we need to convert to array every time an element is added or removed from the TreeSet.
2. By converting to a List (LinkedList or ArrayList)
Instead of converting to an array, we can convert the TreeSet to ArrayList or a LinkedList object and then use the get
method to get the element by index as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.ArrayList; import java.util.List; import java.util.TreeSet; public class GetTreeSetElementsByIndexExample { public static void main(String[] args) { TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(7); tSetOddNumbers.add(3); tSetOddNumbers.add(9); tSetOddNumbers.add(5); tSetOddNumbers.add(1); System.out.println("TreeSet contains: " + tSetOddNumbers); //convert it to an ArrayList List<Integer> listNumbers = new ArrayList<Integer>( tSetOddNumbers ); //access using array index System.out.println("Element at index 0: " + listNumbers.get(0)); System.out.println("Element at index 4: " + listNumbers.get(4)); } } |
Output
1 2 3 |
TreeSet contains: [1, 3, 5, 7, 9] Element at index 0: 1 Element at index 4: 9 |
Note: Similar to the array approach, this approach also needs to allocate a new List containing TreeSet elements. It may not perform well if the TreeSet is large. Additionally, we need to convert TreeSet to a List every time an element is added or removed from the TreeSet.
3. By iterating using an Iterator
The simplest approach is to iterate through the TreeSet elements until we reach the desired index and get the element. We can get the iterator for TreeSet elements using the iterator
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(7); tSetOddNumbers.add(3); tSetOddNumbers.add(9); tSetOddNumbers.add(5); tSetOddNumbers.add(1); System.out.println("TreeSet contains: " + tSetOddNumbers); //get an iterator Iterator<Integer> itr = tSetOddNumbers.iterator(); int desiredIndex = 3; int currentIndex = 0; Integer currentElement = null; //iterate the TreeSet while( itr.hasNext() ){ currentElement = itr.next(); if( currentIndex == desiredIndex ){ System.out.println("Element at index " + desiredIndex + " is: " + currentElement); break; } currentIndex++; } |
Output
1 2 |
TreeSet contains: [1, 3, 5, 7, 9] Element at index 3 is: 7 |
This example is a part of the TreeSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet