subList() in Java

Overview
subList() method belongs to the ArrayList class in Java. This method extracts a portion of the list and returns it. The returned view of portion lies between specified "from" index (inclusive) and "to" index(exclusive). Let us dive in and learn more about this method.
Syntax of subList() in Java
The signature of subList() is defined as :
The following defines how the method is written i.e the syntax :
So as we can see, the method is called on the given ArrayList, to extract the view of the portion between fromIndex and toIndex.
Parameters of subList() in Java
The subList() method takes two parameters:
- int fromIndex: The starting index from which list portion is to be extracted. It is the low endpoint. The element at this index is included in the returned list.
- int toIndex: The ending index till which list portion is to be extracted. It is the high endpoint. The element at this index is excluded from the returned list.
Return Values of subList() in Java
Return Type: List It returns a view of the range of the List between the specified indices.
The returned list is backed by the original list. So, changes in one are reflected in the other.
Exceptions for subList() in Java
subList() method throws the following exceptions:
- IndexOutOfBoundsException: This exception is thrown for an illegal endpoint index value i.e. if fromIndex < 0 or toIndex > size of ArrayList.
- IllegalArgumentException: This exception is thrown if the value of fromIndex is greater than the toIndex.
Examples for both the exceptions are discussed in the “More examples” section.
Example of subList() in Java
Let us consider a short example to understand the implementation better.
Output:
Explanation: In the above program, the subList method is called on employees ArrayList. Using the method, a range of elements from index 1 to 4(1 inclusive, 4 exclusive) is extracted. Both the original list and the sublist are printed.
More about the subList () method in Java
- With the help of thesubList() method, the need for any explicit range operations is eliminated (of the kind that commonly exists for arrays). Any sort of operation/task which expects a list can be used as a range operation by passing a subList view instead of a whole list.
- The returned specified range of list supports all the kinds of operational range operations.
- All types of non-structural changes in the returned range of the list are reflected in the original list and vice-versa.
- In case the fromIndex and toIndex are equal, an empty list is returned.
More Examples
Example 1: Split a Single ArrayList into Two ArrayLists
Output:
Explanation: In the above program, we have used the subList() method to split the employee_age into two arrayLists. All the elements with values less than 40(from indexing 0 to 2) are kept in one list and the ones with values greater than 40(from indexing 3 to end) in another.
Example 2: Remove sublist of arraylist
Output:
Explanation : In the above program, subList() method is called on employee_age list. A specified range from the list is selected between indices 2 to 5. Further, this selected range is removed with the help of the clear() method. The modified list is printed.
Example 3: IndexOutOfBoundsException
Output:
Explanation: In the above program, the size of the employee_age list is 7. The subList() method is called on employee_age, the value of the toIndex parameter passed is 8 which is greater than the size of the list. Hence, IndexOutOfBoundsException is thrown.
Example 4: IllegalArgumentException
Output:
Explanation: In the above program, the subList() method is called on the employee_age list. The parameters passed to the method are out of order. Here, the value of fromIndex is greater than toIndex. Hence, IllegalArgumentException is thrown.
Conclusion
- The subList method belongs to the ArrayList class in Java.
- It returns a specified view of the range of the given list, the indices are passed as a parameter to the method.
- The return type is a List.
- All types of non-structural changes in the returned range of the list are reflected in the original list and vice-versa.
- It is majorly used to eliminate the need for any explicit range operations.