Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java List sublist

farenda 2015-07-23 0

Problem:

How to find a first/last occurrence of sublist in Java List? As expected Java Collections API has a method for this. See the example for details.

Solution:

The following Java code demonstrates use of two similar static methods from great java.util.Collections class:

  • indexOfSubList
    Returns the first occurrence of sublist within a list or -1 when not found.
  • lastIndexOfSubList
    Returns the last occurrence of sublist within a list or -1 when not found.
package com.farenda.java;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsIndexOfSublist {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("This is a sentence".split(""));

        System.out.println("Original list: " + list);

        findFirstAndLastOccurrence(list, Arrays.asList("i", "s"));

        findFirstAndLastOccurrence(list, Collections.emptyList());

        findFirstAndLastOccurrence(list, Arrays.asList("uh", "oh"));
    }

    private static void findFirstAndLastOccurrence(List<String> words,
                                                   List<String> sublist) {
        System.out.printf("The first occurrence of %s at index: %d%n",
                sublist, Collections.indexOfSubList(words, sublist));

        System.out.printf("The last occurrence of %s at index: %d%n%n",
                sublist, Collections.lastIndexOfSubList(words, sublist));
    }
}

Result of running the code is as follows:

Original list: [T, h, i, s,  , i, s,  , a,  , s, e, n, t, e, n, c, e]
The first occurrence of [i, s] at index: 2
The last occurrence of [i, s] at index: 5

The first occurrence of [] at index: 0
The last occurrence of [] at index: 18

The first occurrence of [uh, oh] at index: -1
The last occurrence of [uh, oh] at index: -1

Note that if we are looking for empty sublist, both indexOfSubList and lastIndexOfSubList found it, but on the first and the last index accordingly.

Share with the World!
Categories Java Tags java-collections
Previous: Java Collection to Enumeration
Next: Java Enumeration to List

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored