Java Collections indexOfSubList() Method

The indexOfSubList() method of Java Collections class is used to get the starting position of the first occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list.

Syntax

Following is the declaration of indexOfSubList() method:

Parameter

ParameterDescriptionRequired/Optional
sourceIt is the list in which we search for the first occurrence of target.Required
targetIt is the list to search for as a subList of source.Required

Returns

The indexOfSubList() 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.

Exceptions

NA

Compatibility Version

Java 1.4 and above.

Example 1

Test it Now

Output:

Target list starts at index: 2

Example 2

Test it Now

Output:

Target list starts at index: 2

Example 3

Test it Now

Output:

List :[kitu, mani, raj, karthi, rahul, gagan]
IndexOfSubList: 3





Latest Courses