How to get sublist of List in Java?

JavaObject Oriented ProgrammingProgramming

The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. User of a list 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 popular implementation of the List interface.

List interface method subList() can be used to get a sublist of the list. It requires start and end index. This sublist contains the same objects as in original list and changes to sublist will also reflect in original list. In this article, we're discussing subList() method with relevant examples.

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<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
      System.out.println("List: " + list);

      // Get the subList
      List<String> subList = list.subList(2, 4);

      System.out.println("SubList(2,4): " + subList);
   }
}

Output

This will produce the following result −

List: [a, b, c, d, e]
SubList(2,4): [c, d]

Example 2

Following example shows using sublist() has sideeffects too. If you modify the sublist, it will impact the original list as well as shown in example −

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<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));

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

      // Get the subList
      List<String> subList = list.subList(2, 4);
      System.out.println("SubList(2,4): " + 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: [a, b, c, d, e]
SubList(2,4): [c, d]
SubList: []
List: [a, b, e]
raja
Updated on 26-May-2022 13:25:38

Advertisements