Collections.lastIndexOfSubList(List, List) in Java

Collections.lastIndexOfSubList(List, List): This method is available in java.util.Collections class of Java.

Syntax:

int java.util.Collections.lastIndexOfSubList(List<?> source, List<?> target)

This method takes two arguments. This method returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

Parameters: Two parameters are required for this method.

source: the list in which to search for the last occurrence of the target.

target: the list to search for as a subList of the source.

Returns: the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

Exceptions: NA

Approach

Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionslastIndexOfSubList {
    public static void main(String[] args) {

        List<Integer> arrlist = new ArrayList<Integer>();

        arrlist.add(12);
        arrlist.add(10);

        arrlist.add(5);
        arrlist.add(6);
        arrlist.add(17);

        arrlist.add(10);

        arrlist.add(5);
        List<Integer> arrlist2 = new ArrayList<Integer>();

        arrlist2.add(10);
        arrlist2.add(5);
        System.out.println(Collections.lastIndexOfSubList(arrlist,
arrlist2));

    }
}

Output:

5


No comments:

Post a Comment