
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to get Sublist of an ArrayList using Java?
The List interface is a Collection and it stores a sequence of elements. ArrayList is the most popular implementation of the List interface. A list provides user has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most common implementation of List interface.
Java List provides a method subList() which returns the portion of the list based on start and end index provided. In this article, we'll see the usage of subList() method to get the sublist from an ArrayList.
Syntax
List<E> subList(int fromIndex, int toIndex)
Notes
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
If fromIndex and toIndex are equal, the returned list is empty.
The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported by this list.
Parameters
fromIndex - Low endpoint (inclusive) of the subList.
toIndex - High endpoint (exclusive) of the subList.
Returns
A view of the specified range within this list.
Throws
IndexOutOfBoundsException - For an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
Example 1
Following is the example showing how to get a sublist from a list. −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9)); System.out.println("List: " + list); // Get the subList List<Integer> subList = list.subList(3, 9); System.out.println("SubList: " + subList); } }
Output
This will produce the following result −
List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] SubList: [3, 4, 5, 6, 7, 8]
Example 2
Following is the example showing the side-effects of sublist when created using subList() method from a list. −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9)); System.out.println("List: " + list); // Get the subList List<Integer> subList = list.subList(3, 9); System.out.println("SubList: " + subList); // Clear the sublist subList.clear(); System.out.println("SubList: " + subList); // Original list is also impacted. System.out.println("List: " + list); } }
Output
This will produce the following result −
List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] SubList: [3, 4, 5, 6, 7, 8] SubList: [] List: [0, 1, 2, 9]
- Related Articles
- How to remove a SubList from an ArrayList in Java?
- How to get sublist of List in Java?
- Get the size of an ArrayList in Java
- How to get ArrayList to ArrayList and vice versa in java?
- Get the location of an element in Java ArrayList
- How to implement an ArrayList using JShell in Java 9?
- Get SubList from LinkedList in Java
- How to replace an element of an ArrayList in Java?
- How to reverse an ArrayList in Java?
- How to synchronize an ArrayList in Java?
- Loop through an ArrayList using an Iterator in Java
- Get the index of a particular element in an ArrayList in Java
- How to create an ArrayList from an Array in Java?
- How to get first and last elements from ArrayList in Java?
- How to remove duplicates from an ArrayList in Java?
