Java Program to Find the Index of the TreeSet Element
Last Updated :
27 Jun, 2021
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:
- By converting TreeSet to a List
- Using an Iterator
- 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
Java
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet = new TreeSet<Integer>();
treeSet.add( 34 );
treeSet.add( 23 );
treeSet.add( 43 );
treeSet.add( 41 );
treeSet.add( 35 );
treeSet.add( 33 );
System.out.println( "TreeSet contains: " + treeSet);
System.out.println( "Index of 23: "
+ indexOf(treeSet, 23 ));
System.out.println( "Index of 43: "
+ indexOf(treeSet, 43 ));
System.out.println( "Index of 35: "
+ indexOf(treeSet, 35 ));
System.out.println( "Index of 55: "
+ indexOf(treeSet, 55 ));
}
private static int indexOf(TreeSet<Integer> set,
Integer element)
{
List<Integer> list = new ArrayList<Integer>(set);
return list.indexOf(element);
}
}
|
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:
- Iterator over TreeSet elements using the iterator method.
- 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
Java
import java.util.Iterator;
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet = new TreeSet<Integer>();
treeSet.add( 34 );
treeSet.add( 23 );
treeSet.add( 43 );
treeSet.add( 41 );
treeSet.add( 35 );
treeSet.add( 33 );
System.out.println( "TreeSet contains: " + treeSet);
System.out.println( "Index of 23: "
+ indexOf(treeSet, 23 ));
System.out.println( "Index of 43: "
+ indexOf(treeSet, 43 ));
System.out.println( "Index of 35: "
+ indexOf(treeSet, 35 ));
System.out.println( "Index of 55: "
+ indexOf(treeSet, 55 ));
}
private static int indexOf(TreeSet<Integer> set,
Integer element)
{
int index = - 1 ;
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
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
Java
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet = new TreeSet<Integer>();
treeSet.add( 34 );
treeSet.add( 23 );
treeSet.add( 43 );
treeSet.add( 41 );
treeSet.add( 35 );
treeSet.add( 33 );
System.out.println( "TreeSet contains: " + treeSet);
System.out.println( "Index of 23: "
+ indexOf(treeSet, 23 ));
System.out.println( "Index of 43: "
+ indexOf(treeSet, 43 ));
System.out.println( "Index of 35: "
+ indexOf(treeSet, 35 ));
System.out.println( "Index of 55: "
+ indexOf(treeSet, 55 ));
}
private static int indexOf(TreeSet<Integer> set,
Integer element)
{
int index = - 1 ;
if (set.contains(element)) {
index = set.headSet(element).size();
}
return index;
}
}
|
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
Share your thoughts in the comments
Please Login to comment...