beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to get sublist of an ArrayList with example

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we will see how to get a sublist from an existing ArrayList. We will be doing it using the subList method of ArrayList class.

List subList(int fromIndex, int toIndex)

Here fromIndex is inclusive and toIndex is exclusive. There are few important points regarding this method which I have shared at the end of this post.

Example of getting sub-list from an ArrayList

Points to Note in the below example:
The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example. On the other side if we are storing the returned sublist into a list then there is no need to type cast (Refer the example).

package beginnersbook.com;
import java.util.ArrayList;
import java.util.List;
public class SublistExample {

 public static void main(String a[]){
     ArrayList<String> al = new ArrayList<String>();

     //Addition of elements in ArrayList
     al.add("Steve");
     al.add("Justin");
     al.add("Ajeet");
     al.add("John");
     al.add("Arnold");
     al.add("Chaitanya");

     System.out.println("Original ArrayList Content: "+al);

     //Sublist to ArrayList
     ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4));
     System.out.println("SubList stored in ArrayList: "+al2);

     //Sublist to List
     List<String> list = al.subList(1, 4);
     System.out.println("SubList stored in List: "+list);
  }
}

Output:

Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya]
SubList stored in ArrayList: [Justin, Ajeet, John]
SubList stored in List: [Justin, Ajeet, John]

Note:
The subList method throws IndexOutOfBoundsException – if the specified indexes are out of the range of ArrayList (fromIndex < 0 || toIndex > size).
IllegalArgumentException – if the starting index is greater than the end point index (fromIndex > toIndex).

Enjoyed this post? Try these related posts

  1. Java – HashMap Iterator example
  2. Java Iterator with examples
  3. Java ArrayList indexOf() Method example
  4. Java ArrayList get() Method example
  5. Java ArrayList ensureCapacity() Method example
  6. Difference between List and Set in Java

Comments

  1. Abhishek Basu says

    January 30, 2017 at 7:34 PM

    //Sublist to ArrayList
    ArrayList al2 = new ArrayList(al.subList(1, 4));

    How is the above method working? I know that the below would not work because of downcasting issues.

    ArrayList al2 = (ArrayList) (al.subList(1, 4));

    Reply
  2. Paal says

    September 28, 2017 at 9:01 AM

    Lets say I have a list of unknown length I want to get the whole list except the last element. Will it work to do this:
    ArrayList al2 = new ArrayList(al.subList(1, -2));

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap