JavaTuples indexOf() method

The indexOf() method in org.javatuples is used to find the index of a value, passed as parameter, in TupleClass. This method takes the parameter of Object type, so that it can check for all type of values. It is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns an int which is the first index of the value passed as the parameter, if found. If not found, then it returns -1.

Method Declaration:

public final int indexOf(Object value)

Syntax:



int index = TupleClassObject.indexOf(Object value)

Here TupleClassObject represents the JavaTuple Class object used like Unit, Quintet, Decade, etc.

Return Value: This method returns an int which is the first index of the value passed as the parameter, if found. If not found, then it returns -1.

Below programs illustrate the various ways to use indexOf() method:

Program 1: Using indexOf() with Unit class:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to use indexOf() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an Unit with one value
        Unit<String> unit = Unit.with("GeeksforGeeks");
  
        // Using indexOf() method for present value
        int index1 = unit.indexOf("GeeksforGeeks");
  
        // Printing the index1
        System.out.println("Index of GeeksforGeeks = "
                           + index1);
  
        // Using indexOf() method for absent value
        int index2 = unit.indexOf("Present");
  
        // Printing the index2
        System.out.println("Index of Present = "
                           + index2);
    }
}

chevron_right


Output:

Index of GeeksforGeeks = 0
Index of Present = -1

Program 2: Using indexOf() with Quartet class:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to use indexOf() method
  
import java.util.*;
import org.javatuples.Quartet;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a quartet
        Quartet<Integer, String, String, Double> quartet
            = Quartet.with(Integer.valueOf(1),
                           "GeeksforGeeks",
                           "A computer portal",
                           Double.valueOf(20.18));
  
        // Using indexOf() method for present value
        int index1 = quartet.indexOf("GeeksforGeeks");
  
        // Printing the index1
        System.out.println("Index of GeeksforGeeks = "
                           + index1);
  
        // Using indexOf() method for absent value
        int index2 = quartet.indexOf(5);
  
        // Printing the index2
        System.out.println("Index of 5 = "
                           + index2);
    }
}

chevron_right


Output:

Index of GeeksforGeeks = 1
Index of 5 = -1

Note: Similarly, it can be used with any other JavaTuple Class.



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.