This example shows how to find an index of the specified element in Java TreeSet. The example also shows how to find the element index using the headSet method, iterator, and List.
How to find the element index in Java TreeSet?
Unlike the List classes like ArrayList or a LinkedList, the TreeSet class does not allow accessing elements using the index. There are no direct methods to access the TreeSet elements using the index and thus finding an index of an element is not straight forward. However, we can find the index using below given approaches.
1. Using the headSet method of the TreeSet class
The headSet
method of the TreeSet class returns a view of part of the TreeSet whose elements are less than the specified element.
1 |
public SortedSet<E> headSet(E toElement) |
Since the elements of the TreeSet are automatically sorted either in the natural order of the elements or by a custom comparator, the headset size will be equal to the number of elements that are smaller or lower than the specified element. If we were to put the TreeSet elements in a List, then that number would be equal to the index of the element.
For example, if the TreeSet contains [1, 2, 3, 4] then the headset of element 3 will contain elements [1, 2]. The size of the headset will be 2 and that will be the index of element 3.
The below given example shows how to implement the TreeSet indexOf method using this approach.
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 31 32 33 34 35 36 37 38 39 40 |
import java.util.TreeSet; public class TreeSetFindElementIndexExample { public static void main(String[] args) { TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(34); tSetNumbers.add(23); tSetNumbers.add(43); tSetNumbers.add(41); tSetNumbers.add(35); tSetNumbers.add(33); System.out.println("TreeSet contains: " + tSetNumbers); System.out.println( "Index of 23: " + indexOf(tSetNumbers, 23) ); System.out.println( "Index of 43: " + indexOf(tSetNumbers, 43) ); System.out.println( "Index of 35: " + indexOf(tSetNumbers, 35) ); System.out.println( "Index of 55: " + indexOf(tSetNumbers, 55) ); } private static int indexOf(TreeSet<Integer> set, Integer element){ int index = -1; //if the element exists in the TreeSet if(set.contains(element)){ /* * The element index will be equal to the * size of the headSet for the element */ index = set.headSet(element).size(); } return index; } } |
Output
1 2 3 4 5 |
TreeSet contains: [23, 33, 34, 35, 41, 43] Index of 23: 0 Index of 43: 5 Index of 35: 3 Index of 55: -1 |
2. By converting it to a List
The List class like ArrayList or a LinkedList provides the indexOf
method to find the element index. We can convert the TreeSet to ArrayList and then use the indexOf
method 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 27 28 29 30 31 32 33 34 |
import java.util.ArrayList; import java.util.List; import java.util.TreeSet; public class TreeSetFindElementIndexExample { public static void main(String[] args) { TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(34); tSetNumbers.add(23); tSetNumbers.add(43); tSetNumbers.add(41); tSetNumbers.add(35); tSetNumbers.add(33); System.out.println("TreeSet contains: " + tSetNumbers); System.out.println( "Index of 23: " + indexOf(tSetNumbers, 23) ); System.out.println( "Index of 43: " + indexOf(tSetNumbers, 43) ); System.out.println( "Index of 35: " + indexOf(tSetNumbers, 35) ); System.out.println( "Index of 55: " + indexOf(tSetNumbers, 55) ); } private static int indexOf(TreeSet<Integer> set, Integer element){ //convert TreeSet to ArrayList or LinkedList List<Integer> list = new ArrayList<Integer>(set); //use the indexOf method of the List return list.indexOf(element); } } |
Output
1 2 3 4 5 |
TreeSet contains: [23, 33, 34, 35, 41, 43] Index of 23: 0 Index of 43: 5 Index of 35: 3 Index of 55: -1 |
Note: This approach unnecessarily creates a new List from the TreeSet to find an element index. If the TreeSet has a very large number of elements, this can slow down the performance of the code.
3. Using an Iterator
We can get the Iterator over TreeSet elements using the iterator
method. Once we get an iterator, we can iterate through the elements and search for the specified element 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.Iterator; import java.util.TreeSet; public class TreeSetFindElementIndexExample { public static void main(String[] args) { TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(34); tSetNumbers.add(23); tSetNumbers.add(43); tSetNumbers.add(41); tSetNumbers.add(35); tSetNumbers.add(33); System.out.println("TreeSet contains: " + tSetNumbers); System.out.println( "Index of 23: " + indexOf(tSetNumbers, 23) ); System.out.println( "Index of 43: " + indexOf(tSetNumbers, 43) ); System.out.println( "Index of 35: " + indexOf(tSetNumbers, 35) ); System.out.println( "Index of 55: " + indexOf(tSetNumbers, 55) ); } private static int indexOf(TreeSet<Integer> set, Integer element){ int index = -1; //get an iterator Iterator<Integer> itr = set.iterator(); Integer currentElement = null; int currentIndex = 0; while(itr.hasNext()){ currentElement = itr.next(); if(currentElement.equals(element)){ return currentIndex; } currentIndex++; } return index; } } |
Output
1 2 3 4 5 |
TreeSet contains: [23, 33, 34, 35, 41, 43] Index of 23: 0 Index of 43: 5 Index of 35: 3 Index of 55: -1 |
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