Home »
Java programming language
Java Collections lastlastIndexOfSubList() Method with Example
Collections Class lastIndexOfSubList() method: Here, we are going to learn about the lastIndexOfSubList() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 03, 2020
Collections Class lastIndexOfSubList() method
- lastIndexOfSubList() method is available in java.util package.
- lastIndexOfSubList() method is used to return the starting index of the last occurrence of the given (dest) list within the given source list (src).
- lastIndexOfSubList() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- lastIndexOfSubList() method does not throw an exception at the time of returning the index of the last occurrence of the given List (dest).
Syntax:
public static int lastIndexOfSubList(List src, List dest);
Parameter(s):
- List src – represents the source list in which to filter the last occurrence of the given list(dest).
- List dest – represents the targeted list(dest) to filter sublist of the given source list(src).
Return value:
The return type of this method is int, it returns starting index of the last occurrence of the given sublist (dest) within the given source list(src) otherwise it returns -1 when no search found or list empty.
Example:
// Java program is to demonstrate the example
// of int lastIndexOfSubList() of Collections
import java.util.*;
public class LastIndexOfSubList {
public static void main(String args[]) {
// Instantiate a LinkedList
List src_l = new LinkedList();
List dest_l = new LinkedList();
// By using add() method is to
// add elements in linked list src_l
src_l.add(10);
src_l.add(20);
src_l.add(30);
src_l.add(40);
src_l.add(50);
// By using add() method is to
// add elements in linked list dest_l
dest_l.add(40);
dest_l.add(50);
// Display LinkedList
System.out.println("link_l: " + src_l);
System.out.println("dest_l: " + dest_l);
System.out.println();
// By using lastIndexOfSubList() method is to
// return the starting index of last occurrence
// of dest_l in src_l
int index = Collections.lastIndexOfSubList(src_l, dest_l);
//Display index
System.out.println("Collections.lastIndexOfSubList(src_l,dest_l): " + index);
}
}
Output
link_l: [10, 20, 30, 40, 50]
dest_l: [40, 50]
Collections.lastIndexOfSubList(src_l,dest_l): 3