AbstractList subList() method in Java with Examples
Last Updated :
22 Aug, 2019
The
subList() method of
java.util.AbstractList class is used to return 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.
Syntax:
public List<E> subList(int fromIndex, int toIndex)
Parameters: This method takes the following argument as a parameter.
- fromIndex: low endpoint (inclusive) of the subList
- toIndex: high endpoint (exclusive) of the subList
Returns Value: This method returns a
view of the specified range within this list.
Exception: This method throws the following Exception.
- IndexOutOfBoundsException: if an endpoint index value is out of range (fromIndex size)
- IllegalArgumentException: if the endpoint indices are out of order (fromIndex > toIndex)
Below are the examples to illustrate the subList() method:
Example 1:
Java
// Java program to demonstrate
// subList() method for String value
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of AbstractList<Integer>
AbstractList<String>
arrlist = new ArrayList<String>();
// Populating arrlist1
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
arrlist.add("E");
// print arrlist
System.out.println("Original AbstractList: "
+ arrlist);
// getting the subList
// using subList() method
List<String> arrlist2 = arrlist.subList(2, 4);
// print the subList
System.out.println("Sublist of AbstractList: "
+ arrlist2);
}
catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
catch (IllegalArgumentException e) {
System.out.println(e);
}
}
}
Output:
Original AbstractList: [A, B, C, D, E]
Sublist of AbstractList: [C, D]
Example 2: For IndexOutOfBoundsException
Java
// Java program to demonstrate
// subList() method for IndexOutOfBoundsException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of AbstractList<Integer>
AbstractList<String>
arrlist = new ArrayList<String>();
// Populating arrlist1
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
arrlist.add("E");
// print arrlist
System.out.println("Original AbstractList: "
+ arrlist);
// getting the subList
// using subList() method
System.out.println("\nEnd index value is out of range");
List<String> arrlist2 = arrlist.subList(2, 7);
// print the subList
System.out.println("Sublist of AbstractList: "
+ arrlist2);
}
catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
catch (IllegalArgumentException e) {
System.out.println(e);
}
}
}
Output:
Original AbstractList: [A, B, C, D, E]
End index value is out of range
java.lang.IndexOutOfBoundsException: toIndex = 7
Example 3: For
IllegalArgumentException
Java
// Java program to demonstrate
// subList() method for IllegalArgumentException
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// Creating object of AbstractList<Integer>
AbstractList<String>
arrlist = new ArrayList<String>();
// Populating arrlist1
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
arrlist.add("E");
// print arrlist
System.out.println("Original AbstractList: "
+ arrlist);
// getting the subList
// using subList() method
System.out.println("\nEndpoint indices "
+ "are out of order"
+ " (fromIndex > toIndex)");
List<String> arrlist2 = arrlist.subList(7, 2);
// print the subList
System.out.println("Sublist of AbstractList: "
+ arrlist2);
}
catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
catch (IllegalArgumentException e) {
System.out.println(e);
}
}
}
Output:
Original AbstractList: [A, B, C, D, E]
Endpoint indices are out of order (fromIndex > toIndex)
java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)
Similar Reads
AbstractList in Java with Examples The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Rand
7 min read
AbstractList add(E ele) method in Java with Examples The add(E ele) method of AbstractList class in Java is used to insert the specified element to the end of the current list. Syntax: public boolean add(E ele) Where E is the type of element maintained by this AbstractList collection. Parameter: This method accepts a single parameter ele which represe
2 min read
AbstractList addAll() method in Java with Examples The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem
5 min read
AbstractList clear() method in Java with Examples The clear() method of java.util.AbstractList class is used to remove all of the elements from this list. The list will be empty after this call returns. Syntax: public void clear() Returns Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1
2 min read
AbstractList equals() method in Java with Examples The equals() method of java.util.AbstractList class is used to compare the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e
3 min read
AbstractList get() method in Java with Examples The get() method of java.util.AbstractList class is used to return the element at the specified position in this list. Syntax: public abstract E get(int index) Parameters: This method takes index of the element as a parameter, the element at which is to be returned. Returns Value: This method return
2 min read
AbstractList hashCode() method in Java with Examples The hashCode() method of java.util.AbstractList class is used to return the hash code value for this list. Syntax: public int hashCode() Returns Value: This method returns the hash code value for this list. Below are the examples to illustrate the hashCode() method. Example 1: Java // Java program t
2 min read
AbstractList indexOf() method in Java with Examples The indexOf() method of java.util.AbstractList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if t
3 min read
AbstractList iterator() method in Java with Examples The iterator() method of java.util.AbstractList class is used to return an iterator over the elements in this list in proper sequence. This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's size(), get(int), and remove(int) methods. Synt
2 min read
AbstractList lastIndexOf() method in Java with Examples The lastIndexOf() method of java.util.AbstractList class is used to return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1
2 min read