
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
How to get sublist of List in Java?
In Java, a List is an interface that stores elements of the same type. You can retrieve a sub-list from a List in Java. A sub-list is the view of a portion (or part) of the List. For example, if the given list is {a, b, c, d, e}, then the possible sub-lists can be {a, b}, {a, b, c}, {b, c, d}, {d, e}, etc.
The List interface in Java provides a built-in method named subList(), which returns a sub-list from the given List. We just need to specify the range of extraction.
Sublist from a List 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 List {1, 2, 3, 4, 5} between the specified range 0 to 4:
import java.util.ArrayList; import java.util.ArrayList; import java.util.List; public class getSublist { public static void main(String[] args) { //instantiating a List using ArrayList class List<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 list is: " + list); //range int fromIndex = 0; 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 list is: [1, 2, 3, 4, 5] The range is: 0, 4 The sublist elements are: [1, 2, 3, 4]
Adding a List Element to another List
We can get a sub-list by adding a specific range of elements from one List to another List. The resultant list 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 a List, we need to iterate through the (original) list at any position using a for loop and add those elements to a new list (i.e., sub-list) using the add() method.
Example
In the example below, we add elements from the original List to a new List (i.e., sub-list) from index 1. To do this, we iterate through the original list starting from index 1 and add each element to the sub-list using the add() method:
import java.util.ArrayList; import java.util.List; public class getSublist { public static void main(String[] args) { //instantiating a List using an ArrayList class List<String> vowels = new ArrayList<>(); //adding elements vowels.add("A"); vowels.add("E"); vowels.add("I"); vowels.add("O"); vowels.add("U"); System.out.println("The original list is: " + vowels); //creating another List List<String> subList = new ArrayList<>(); for(int i = 1; i<vowels.size(); i++){ subList.add(vowels.get(i)); } System.out.println("The subList elements are: " + subList); } }
Following is the output of the above program:
The original list is: [A, E, I, O, U] The subList elements are: [E, I, O, U]