subList() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

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.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.

Sharpen Your Fundamentals with Free Learning

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.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.