coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

subList() method of ArrayList class in java

In this tutorial, we will see the subList() method of the ArrayList class in Java. This method returns a view of the specified range within the list.

Introduction

ArrayList is a widely used class to store and retrieve data in a collection framework.

ArrayList is an ordered collection i.e. it maintains the insertion order of the elements. Also, it allows duplicate elements in the list.

Also, we have seen a few methods of the ArrayList class before like add(), addAll(), remove(), set(), iterate(), clear(), contains(), get(), removeRange() and retainAll() method.

Today we will see the subList(int fromIndex, int toIndex) method.

ArrayList subList(int fromIndex, int toIndex) method

The subList(int fromIndex, int toIndex) method returns the view of the portion of this list between the specified fromIndex, inclusive and toIndex, exclusive.

The method signature is given below.

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

As shown in the method signature, the subList() method accepts two parameters of type int i.e fromIndex and toIndex

Parameter Description
fromIndex low endpoint (inclusive) of the subList
toIndex high endpoint (exclusive) of the subList

Let’s see the Java program for a better understanding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.ArrayList;
import java.util.List;

/**
 * A java program to explain the subList() method of the ArrayList class in Java
 * 
 * @author coderolls.com
 *
 */
public class ArrayListSubListExample {

  public static void main(String[] args) {
    // create an empty ArrayList object 'states'
    List<String> states = new ArrayList<String>();
    
    // add state in the ArrayList
    states.add("California");
    states.add("Texas");
    states.add("Florida");
    states.add("New Jersey");
    states.add("Washington");
    
    System.out.println("The states list: \n"+ states);
    
    //Sublist the states list from 1 to 3 index
    List<String> statesSubList = states.subList(1, 3);
    
    System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
  }
}

Output:

1
2
3
4
5
The states list: 
[California, Texas, Florida, New Jersey, Washington]

The states sublist from index 1 (inclusive) to 3 (exclusive): 
[Texas, Florida]

Exceptions thrown by subList() Methods

This method may throw the ClassCastException and NullPointerException.

IndexOutOfBoundsException

This method may throw the ClassCastException, if an endpoint index value is out of range. i.e. if fromIndex is less than 0 (fromIndex < 0 ) or toIndex is greater than the size of the ArrayList (toIndex > size).

The Java example for the IndexOutOfBoundsException is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * A Java program to explain the subList() methods 
 * IndexOutOfBoundsException exception case.
 * 
 * @author coderolls.com
 */
public class SubListMethodExceptionExample1 {

  public static void main(String[] args) {
    // create an empty ArrayList object 'states'
    List<String> states = new ArrayList<String>();
    
    // add state in the ArrayList
    states.add("California");
    states.add("Texas");
    states.add("Florida");
    states.add("New Jersey");
    states.add("Washington");
    
    System.out.println("The states list: \n"+ states);
    
    // will throw IndexOutOfBoundsException
    List<String> statesSubList = states.subList(1, 7); // will throw exception
    
    System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
  }
}

Output:

1
2
3
4
5
6
The states list: 
[California, Texas, Florida, New Jersey, Washington]
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 7
	at java.util.ArrayList.subListRangeCheck(ArrayList.java:1012)
	at java.util.ArrayList.subList(ArrayList.java:1004)
	at com.gaurav.ExProject.ArrayList.SubListMethodExceptionExample1.main(SubListMethodExceptionExample1.java:22)

IllegalArgumentException

This method may throw the NullPointerException, if the endpoint indices are out of order. i.e. if the fromIndex is greater than toIndex

The Java example for the IllegalArgumentException is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * A Java program to explain the subList() methods 
 * IllegalArgumentException exception case.
 * 
 * @author coderolls.com
 */
public class SubListMethodExceptionExample2 {

  public static void main(String[] args) {
    // create an empty ArrayList object 'states'
    List<String> states = new ArrayList<String>();
    
    // add state in the arraylist
    states.add("California");
    states.add("Texas");
    states.add("Florida");
    states.add("New Jersey");
    states.add("Washington");
    
    System.out.println("The states list: \n"+ states);
    
    // will throw IllegalArgumentException
    List<String> statesSubList = states.subList(3, 1); 
    
    System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
  }
}

Output:

1
2
3
4
5
6
The states list: 
[California, Texas, Florida, New Jersey, Washington]
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(3) > toIndex(1)
	at java.util.ArrayList.subListRangeCheck(ArrayList.java:1014)
	at java.util.ArrayList.subList(ArrayList.java:1004)
	at com.gaurav.ExProject.ArrayList.SubListMethodExceptionExample2.main(SubListMethodExceptionExample2.java:28)

Conclusion

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

The subList(int fromIndex, int toIndex) method returns the view of the portion of this list between the specified fromIndex, inclusive and toIndex, exclusive.


The example Java program used in the above article can be found at this GitHub repository.

Please write your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus