ArrayList

Get sub list of ArrayList example

This is an example of how to get a subList of an ArrayList, that is a list containing elements from a startIndex to an endIndex of the ArrayList. Getting a subList of an ArrayList implies that you should:

  • Create a new ArrayList.
  • Populate the arrayList with elements, using add(E e) API method of ArrayList.
  • Use the subList(int startIndex, int endIndex) API method of ArrayList to get a sub list of the original ArrayList. The method returns a List containing elements from startIndex to endIndex - 1 of the original ArrayList.
  • Note that the sub List returned is backed by the original Arraylist. So any changes made to the sub list will also be reflected to the original ArrayList. We can check that by removing an element from the sub list and then get the elements in the arrayList. The element removed from the sub list will no longer exist in the arrayList also.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.ArrayList;
import java.util.List;
 
public class GetSubListOfJavaArrayList {
 
  public static void main(String[] args) {
 
    // Create an ArrayList and populate it with elements
    ArrayList arrayList = new ArrayList();
    arrayList.add("element_1");
    arrayList.add("element_2");
    arrayList.add("element_3");
    arrayList.add("element_4");
    arrayList.add("element_5");
 
    /*

 Use the List subList(int startIndex, int endIndex) operation 

 to get a sub list of the original ArrayList. This method 

 returns an List object containing elements from startIndex 

 to endIndex - 1 of the original ArrayList.
    */
    List subList = arrayList.subList(1,3);
 
    System.out.println("Elements in sub list :");
    for(int i=0; i < subList.size() ; i++)

System.out.println(subList.get(i));
 
    /*

Sub List returned is backed by original Arraylist. So any

changes made to sub list will also be reflected to the 

original ArrayList. We will test that by removing an element 

from the sub list and check that it is removed from the original 

ArrayList also
    */
    Object obj = subList.remove(0);
    System.out.println(obj + " is removed from sub list");
 
    System.out.println("Elements in ArrayList :");
    for(int i=0; i < arrayList.size() ; i++)

System.out.println(arrayList.get(i));
 
  }
 
}

Output:

Elements in sub list :
element_2
element_3
element_2 is removed from sub list
Elements in ArrayList :
element_1
element_3
element_4
element_5

 
This was an example of how to get a subList of an ArrayList in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button