How to get Sublist of an ArrayList using Java?


The List interface is a Collection and it stores a sequence of elements. ArrayList is the most popular implementation of the List interface. A list provides user has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most common implementation of List interface.

Java List provides a method subList() which returns the portion of the list based on start and end index provided. In this article, we'll see the usage of subList() method to get the sublist from an ArrayList.

Syntax

List<E> subList(int fromIndex, int toIndex)

Notes

  • Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

  • If fromIndex and toIndex are equal, the returned list is empty.

  • The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.

  • The returned list supports all of the optional list operations supported by this list.

Parameters

  • fromIndex - Low endpoint (inclusive) of the subList.

  • toIndex - High endpoint (exclusive) of the subList.

Returns

A view of the specified range within this list.

Throws

  • IndexOutOfBoundsException - For an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex)

Example 1

Following is the example showing how to get a sublist from a list. −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9));
      System.out.println("List: " + list);

      // Get the subList
      List<Integer> subList = list.subList(3, 9);
      System.out.println("SubList: " + subList);
   }
}

Output

This will produce the following result −

List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
SubList: [3, 4, 5, 6, 7, 8]

Example 2

Following is the example showing the side-effects of sublist when created using subList() method from a list. −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9));
      System.out.println("List: " + list);

      // Get the subList
      List<Integer> subList = list.subList(3, 9);
      System.out.println("SubList: " + subList);

      // Clear the sublist
      subList.clear();
      System.out.println("SubList: " + subList);

      // Original list is also impacted.
      System.out.println("List: " + list);
   }
}

Output

This will produce the following result −

List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
SubList: [3, 4, 5, 6, 7, 8]
SubList: []
List: [0, 1, 2, 9]

Updated on: 26-May-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements