
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Sublist of an ArrayList Using Java
In Java, an ArrayList is a class of the List interface, which stores elements of similar data types and can be null while creating.
A sub-list is the view of a portion (or part) of an ArrayList. For example, if the given array list is {1, 2, 3, 4, 5}, then the possible sub-lists can be {1, 2}, {1, 2, 3}, {2, 3, 4}, {4, 5}, etc.
The List interface in Java provides a built-in method named subList(), which directly returns a sub-list from the given ArrayList.
Sublist from an ArrayList using the subList() Method
The subList() method of the List interface returns a portion (i.e., sub-list) of the original list between the specified range, which is fromIndex to toIndex. In the obtained sub-list, the element at fromIndex will be included, and at toIndex will be excluded.
Following is the syntax of the subList() method:
List<E> subList(int fromIndex, int toIndex)
Here,
- fromIndex: The starting index (inclusive) from which the sub-list will begin extracting elements.
- toIndex: The last index (not included) up to which the sub-list will be extracted.
Example
In the following example, we use the subList() method to get a sub-list from an ArrayList {10, 20, 30, 40, 50} between the specified range 1 to 5:
import java.util.ArrayList; import java.util.List; public class findElement { public static void main(String[] args) { //creating an ArrayList ArrayList<Integer> list = new ArrayList<>(); //adding elements list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println("The original ArrayList is: " + list); //range int fromIndex = 1; int toIndex = 4; System.out.println("The range is: " + fromIndex + ", " + toIndex); // Get the subList using subList() method List<Integer> subList = list.subList(fromIndex, toIndex); System.out.println("The sublist elements are: " + subList); } }
Below is the output of the above program:
The original ArrayList is: [10, 20, 30, 40, 50] The range is: 1, 4 The sublist elements are: [20, 30, 40]
Adding an ArrayList Element to another ArrayList
We can get a sub-list by adding a specific range of elements from one ArrayList to another ArrayList. The resultant ArrayList will be the desired sub-list.
The add() method of the List interface appends the specified element to the end of the list. Therefore, to get a sublist of an ArrayList, we need to iterate through the (original) ArrayList at any position using a for loop and add those elements to a new ArrayList (i.e., sub-list) using the add() method.
Example
In the example below, we add elements from the original ArrayList to a new ArrayList (i.e., sub-list) from index 2. To do this, we iterate through the original ArrayList starting from index 2 and add each element to the sub-list using the add() method.
import java.util.ArrayList; public class findElement { public static void main(String[] args) { //creating an ArrayList ArrayList<Integer> list = new ArrayList<>(); //adding elements list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println("The original ArrayList is: " + list); //creating another ArrayList ArrayList<Integer> subList = new ArrayList<>(); for(int i = 2; i<list.size(); i++){ subList.add(list.get(i)); } System.out.println("The subList elements are: " + subList); } }
Following is the output of the above program:
The original ArrayList is: [1, 2, 3, 4, 5] The subList elements are: [3, 4, 5]