We can get sublist from arraylist using subList() method by passing the start and end index of the range.
Syntax:
arrayList.subList(startIndex, endIndex);
Example:
package com.w3schools; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); List<String> list = arrayList.subList(1, 4); System.out.println("Sub List: "+list); } } |
Output
Actual ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal] Sub List: [Mahesh, Vivek, Naren] |