Java Guava | Longs.lastIndexOf() method with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The lastIndexOf() method of Longs Class in Guava library is used to find the last index of the given long value in a long array. This long value to be searched and the long array in which it is to be searched, both are passed as a parameter to this method. It returns an integer value which is the last index of the specified long value. If the value is not found, it returns -1. Syntax: public static int lastIndexOf(long[] array, long target) Parameters: This method accepts two mandatory parameters: array: which is the array of long values in which the long value is to searched. target: which is long value to be searched for last index in the long array. Return Value: This method returns an integer value which is the last index of the specified long value. If the value is not found, it returns -1. Below programs illustrates this method: Example 1: Java // Java code to show implementation of // Guava's Longs.lastIndexOf() method import com.google.common.primitives.Longs; import java.util.Arrays; class GFG { // Driver's code public static void main(String[] args) { // Creating a long array long[] arr = { 1, 2, 3, 4, 3, 5, 3, 4 }; long target = 3; // Using Longs.lastIndexOf() method // to get the index of last appearance // of a given element in array // and return -1 if element is // not found in the array int index = Longs.lastIndexOf(arr, target); if (index != -1) { System.out.println("Target is present" + " at index " + index); } else { System.out.println("Target is not present" + " in the array"); } } } Output: Target is present at index 6 Example 2: Java // Java code to show implementation of // Guava's Longs.lastIndexOf() method import com.google.common.primitives.Longs; import java.util.Arrays; class GFG { // Driver's code public static void main(String[] args) { // Creating a long array long[] arr = { 3, 5, 7, 11, 13 }; long target = 17; // Using Longs.lastIndexOf() method // to get the index of last appearance // of a given element in array // and return -1 if element is // not found in the array int index = Longs.lastIndexOf(arr, target); if (index != -1) { System.out.println("Target is present" + " at index " + index); } else { System.out.println("Target is not present" + " in the array"); } } } Output: Target is not present in the array Reference: https://guava.dev/releases/21.0/api/docs/com/google/common/primitives/Longs.html#lastIndexOf-long:A-long- Comment More infoAdvertise with us Next Article Java Guava | Longs.join() method with Examples S Sahil_Bansall Follow Improve Article Tags : Java java-guava Guava-Functions Guava-Longs Practice Tags : Java Similar Reads Java Guava | Longs.join() method with Examples The join() method of Longs Class in the Guava library is used to combine or join all the given long values separated by a separator. These long values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of joi 2 min read Java String lastIndexOf() Method with Examples The String lastIndexOf() method returns the position of the last occurrence of a given character or substring in a String. The Index starts from 0, and if the given substring is not found it returns -1.In this article, we will learn how to use the string lastIndexOf() method in Java to find the last 4 min read Java Guava | Longs.asList() method with Examples The Longs.asList() method of Guava's Longs Class accepts a long array as a parameter and returns a list which has the fixed size. The returned list is backed by the long array which is passed as the argument. It means the changes made in the array passed as parameter will be reflected back in the li 2 min read Stack lastIndexOf() method in Java with Example The Java.util.Stack.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present in the Stack then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used 2 min read Java Guava | Longs.contains() method with Examples The Longs.contains() method of Guava's Longs Class is used to check if a value i.e. target is present in the array or not. This target value and the array are taken as parameters to this method. And this method returns a boolean value that states whether the target value is Syntax: public static boo 2 min read Arraylist lastIndexOf() Method in Java with Examples In Java, the lastIndexOf() method is used to find the index of the last occurrence of a specified element in an ArrayList.Example 1: Here, we will use the lastIndexOf() method to find the index of the last occurrence of a specific element in an ArrayList of integers.Java// Java program to demonstrat 3 min read Like