Java Collections indexOfSubList() Method

The indexOfSubList() method in Java is a utility method that finds the starting position of the first occurrence of a specified target sublist within a given source list. This method is part of the java.util.Collections class and provides a convenient way to locate sublists within a larger list.

Table of Contents

  1. Introduction
  2. indexOfSubList() Method Syntax
  3. Examples
    • Basic Usage of indexOfSubList()
    • Using indexOfSubList() with Custom Classes
  4. Real-World Use Case
  5. Conclusion

Introduction

The Collections.indexOfSubList() method is used to search for a sublist within a larger list and return the starting index of the first occurrence of the sublist. If the sublist is not found, the method returns -1. This method is useful for finding patterns or sequences within lists.

The method performs a linear search, and the comparison is based on the equals() method, ensuring that the elements in both lists are compared according to their type.

indexOfSubList() Method Syntax

The syntax for the indexOfSubList() method is as follows:

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

Parameters:

  • source: The list in which to search for the sublist.
  • target: The sublist to search for within the source list.

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.

Throws:

  • NullPointerException if either the source or target list is null.

Examples

Basic Usage of indexOfSubList()

The following example demonstrates how to use the indexOfSubList() method to find the starting index of a sublist within a list.

Example

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

public class IndexOfSubListExample {
    public static void main(String[] args) {
        // Create a source list
        List<String> sourceList = new ArrayList<>();
        Collections.addAll(sourceList, "Apple", "Banana", "Cherry", "Date", "Banana", "Cherry");

        // Create a target list (sublist)
        List<String> targetList = new ArrayList<>();
        Collections.addAll(targetList, "Banana", "Cherry");

        // Find the starting index of the first occurrence of the target list within the source list
        int index = Collections.indexOfSubList(sourceList, targetList);

        // Display the index
        System.out.println("Index of first occurrence of the target list: " + index);
    }
}

Output:

Index of first occurrence of the target list: 1

Using indexOfSubList() with Custom Classes

You can also use the indexOfSubList() method with lists containing instances of custom classes.

Example

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

class Student {
    String name;

    Student(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Student student = (Student) obj;
        return name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return name;
    }
}

public class CustomIndexOfSubListExample {
    public static void main(String[] args) {
        // Create a source list of students
        List<Student> sourceList = new ArrayList<>();
        sourceList.add(new Student("Amit"));
        sourceList.add(new Student("Neha"));
        sourceList.add(new Student("Raj"));
        sourceList.add(new Student("Neha"));
        sourceList.add(new Student("Raj"));

        // Create a target list (sublist) of students
        List<Student> targetList = new ArrayList<>();
        targetList.add(new Student("Neha"));
        targetList.add(new Student("Raj"));

        // Find the starting index of the first occurrence of the target list within the source list
        int index = Collections.indexOfSubList(sourceList, targetList);

        // Display the index
        System.out.println("Index of first occurrence of the target list: " + index);
    }
}

Output:

Index of first occurrence of the target list: 1

Explanation:

  1. Source List: The initial list contains instances of the Student class.

  2. Target List: The target list is a sublist of the source list, consisting of specific Student instances.

  3. Frequency Count: The indexOfSubList() method is used to find the starting index of the first occurrence of the target list within the source list.

Real-World Use Case

Detecting Patterns in Data Sequences

In real-world applications, the indexOfSubList() method can be used to detect patterns or sequences within data. This is particularly useful in scenarios such as signal processing, data analysis, and sequence detection.

Example

Imagine a scenario where you need to detect a sequence of events in a list of event logs.

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

public class EventLogExample {
    public static void main(String[] args) {
        // Create a list of events (source list)
        List<String> eventLogs = new ArrayList<>();
        Collections.addAll(eventLogs, "Login", "Download", "Upload", "Logout", "Download", "Upload");

        // Create a target list (sublist) of events to detect
        List<String> targetSequence = new ArrayList<>();
        Collections.addAll(targetSequence, "Download", "Upload");

        // Find the starting index of the first occurrence of the target sequence within the event logs
        int index = Collections.indexOfSubList(eventLogs, targetSequence);

        // Display the index
        System.out.println("Index of first occurrence of the target sequence: " + index);
    }
}

Output:

Index of first occurrence of the target sequence: 1

Explanation:

  1. Event Logs: The list contains a sequence of event logs.

  2. Target Sequence: The target sequence is a sublist representing a pattern of events.

  3. Index Detection: The indexOfSubList() method is used to detect the starting index of the first occurrence of the target sequence within the event logs.

Conclusion

The Collections.indexOfSubList() method is a powerful utility for finding the starting index of a sublist within a list in Java. By providing a simple way to search for patterns or sequences, it enhances the flexibility and readability of your code. This method is particularly valuable in scenarios where you need to detect specific patterns or sequences within collections, improving the robustness and maintainability of your Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top