Sunday, February 22, 2026
HomeLanguagesJavaJavaTuple lastIndexOf() method

JavaTuple lastIndexOf() method

The lastIndexOf() method in org.javatuples is used to find the last 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 last index of the value passed as the parameter, if found more than once. If found just once, it returns that index. And if not found, then it returns -1.

Method Declaration:

public final int lastIndexOf(Object value)

Syntax:

int index = TupleClassObject.lastIndexOf(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 last index of the value passed as the parameter, if found more than once. If found just once, it returns that index. And if not found, then it returns -1.

Below are programs that will illustrate the various ways to use lastIndexOf() method:

Program 1: Using lastIndexOf() with Unit class:




// Below is a Java program to use lastIndexOf() 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("Lazyroar");
  
        // Using lastIndexOf() method for present value
        int index = unit.lastIndexOf("Lazyroar");
  
        // Printing the index
        System.out.println("Last Index of Lazyroar = "
                           + index);
  
        // Using lastIndexOf() method for absent value
        index = unit.lastIndexOf("Present");
  
        // Printing the index
        System.out.println("Last Index of Present = "
                           + index);
    }
}


Output:

Last Index of Lazyroar = 0
Last Index of Present = -1

Program 2: Using lastIndexOf() with Quartet class:




// Below is a Java program to use lastIndexOf() method
  
import java.util.*;
import org.javatuples.Quartet;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a quartet
        Quartet<Integer, Integer, Integer, String> quartet
            = Quartet.with(Integer.valueOf(1),
                           Integer.valueOf(1),
                           Integer.valueOf(1),
                           "Lazyroar");
  
        // Using lastIndexOf() method for value
        // present more than once
        int index = quartet.lastIndexOf(1);
  
        // Printing the index
        System.out.println("Last Index of 1 = "
                           + index);
  
        // Using lastIndexOf() method for value
        // present just once
        index = quartet.lastIndexOf("Lazyroar");
  
        // Printing the index
        System.out.println("Last Index of Lazyroar = "
                           + index);
  
        // Using lastIndexOf() method for value
        // not present
        index = quartet.lastIndexOf(20.5);
  
        // Printing the index
        System.out.println("Last Index of 20.5 = "
                           + index);
    }
}


Output:

Last Index of 1 = 2
Last Index of Lazyroar = 3
Last Index of 20.5 = -1

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS