Java Collection Tutorial - Java Collections .indexOfSubList ( List <?> source, List <?> target)








Syntax

Collections.indexOfSubList(List <?> source, List <?> target) has the following syntax.

public static int indexOfSubList(List <?> source,   List <?> target)

Example

In the following code shows how to use Collections.indexOfSubList(List <?> source, List <?> target) method.

//w  w w  .  ja v  a  2 s  .  co  m
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String args[]) {
      // create two array list objects       
      List<String> arrlistsrc = new ArrayList<String>();
      List<String> arrlisttarget = new ArrayList<String>();
      
      // populate two lists
      arrlistsrc.add("A");
      arrlistsrc.add("from java2s.com");
      arrlistsrc.add("C");
      arrlistsrc.add("D");
      arrlistsrc.add("E"); 
      
      arrlisttarget.add("C");
      arrlisttarget.add("D");
      arrlisttarget.add("E");
           
      // check target list in source list
      int index = Collections.indexOfSubList(arrlistsrc, arrlisttarget);
      
      System.out.println("Target list starts at index: "+index);    
   }    
}

The code above generates the following result.