Collections.indexOfSubList(List, List) in Java

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

Syntax:

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

This method takes two arguments. This method returns the starting position of the first 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 first occurrence of the target.

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

Returns: the starting position of the first 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 CollectionsindexOfSubList {
    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);

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

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

    }
}

Output:

1


No comments:

Post a Comment