Find Sublist Index with indexOfSubList()

The Collections class comes with many static methods (known as algorithms) of which two are used to know whether a few elements of a list are available in another list. If available, with these methods it can be known at what index they are available.

The following is the signature of these two methods which support subList() operation.

  • static int indexOfSubList(List source, List target): Checks for the existence of a few elements in another list. Returns the index number of the first occurrence of the target list in the source list. It is like indexOf() of String class. If no elements are found, returns -1. Added to Collections class with JDK 1.4.
  • static int lastIndexOfSubList(List source, List target): Checks for the existence of a few elements in another list. Returns the index number of the last occurrence of the target list in the source list. It is used when the same element exists many times. It is like lastIndexOf() of String class. If no elements are found, returns -1. Added to Collections class with JDK 1.4.
Example to Find Sublist Index with indexOfSubList()
import java.util.*;
public class CollectionsIndexOfSubList
{
  public static void main(String args[])
  {
    ArrayList firstList = new ArrayList();
    firstList.add(10);      
    firstList.add(20);
    firstList.add(30);      
    firstList.add(40);
    firstList.add(100);      
    firstList.add(20);
    firstList.add(30);      
    firstList.add(400);

    ArrayList secondList = new ArrayList();
    secondList.add(20);
    secondList.add(30);      

    System.out.println("firstList elements: " + firstList);
    System.out.println("secondList elements: " + secondList);
    int num1 = Collections.indexOfSubList(firstList, secondList);
    System.out.println("\nIndex of first occurrence of secondList in firstList: " + num1);

    int num2 = Collections.lastIndexOfSubList(firstList, secondList);
    System.out.println("Index of last occurrence of secondList in firstList: " + num2);
  }
}

Find Sublist Index with indexOfSubList()
Output screen on Find Sublist Index with indexOfSubList()

ArrayList firstList = new ArrayList();

An ArrayList firstList is created and added with a few elements where the elements 20 and 30 are repeated (exist in two places).

ArrayList secondList = new ArrayList();
secondList.add(20);
secondList.add(30);

Another List object secondList is created and added with only 20 and 30 elements.

int num1 = Collections.indexOfSubList(firstList, secondList);
System.out.println(“Index of first occurrence of secondList in firstList: ” + num1);

The existence of secondList elements in firstList is checked with indexOfSubList() method. Remember, the secondList elements 20 and 30 are available in firstList at two places. The method returns the index number of first occurrence. It prints 1.

int num2 = Collections.lastIndexOfSubList(firstList, secondList);
System.out.println(“Index of last occurrence of secondList in firstList: ” + num2);

The method lastIndexOfSubList() returns the last occurrence of 20 and 30 in firstList. It prints 5.

Leave a Comment

Your email address will not be published.