VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-find-the-index-of-the-treeset-element/

⇱ Java Program to Find the Index of the TreeSet Element - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Find the Index of the TreeSet Element

Last Updated : 23 Jul, 2025

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 straightforward. 

Methods: There are primarily three standard methods as follows:

  1. By converting TreeSet to a List
  2. Using an Iterator
  3. Using the headSet() method of the TreeSet class

Method 1: By converting TreeSet 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. This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Syntax:

public int indexOf(Object o) ;

Parameters: This function has a single parameter, i.e, the element to be searched in the list.

Returns: This method returns the index of the first occurrence of the given element in the list and returns “-1” if the element is not in the list.

Example

 
 


Output
TreeSet contains: [23, 33, 34, 35, 41, 43]
Index of 23: 0
Index of 43: 5
Index of 35: 3
Index of 55: -1


 

Method 2: Using an Iterator


 

Procedure: 


 

  1. Iterator over TreeSet elements using the iterator method.
  2. Once the iterator is fetched, iterate through the elements and search for the specified element as per requirements either from the user or custom inputs as shown below for understanding purposes.


 

Example


 

 
 


Output
TreeSet contains: [23, 33, 34, 35, 41, 43]
Index of 23: 0
Index of 43: 5
Index of 35: 3
Index of 55: -1


 

Method: 3 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. 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.


 

Illustration:


 

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. Thus, if we get the size of the headset, then it will be equal to the position of the element for which we have to find the index.


 

Example 


 

 
 


Output
TreeSet contains: [23, 33, 34, 35, 41, 43]
Index of 23: 0
Index of 43: 5
Index of 35: 3
Index of 55: -1


 

Comment