The subList() method of AbstractSequentialList in Java


The subList() method returns a part of this list between the specified fromIndex, inclusive, and toIndex, exclusive. Get a sublist using the method by setting the range as the two parameters.

The syntax is as follows −

public List<E> subList(int fromIndex, int toIndex)

Here, the parameter fromIndex is the low endpoint (inclusive) of the subList, whereas toIndex is the high endpoint (exclusive) of the subList

To work with the AbstractSequentialList class in Java, you need to import the following package −

import java.util.AbstractSequentialList;

The following is an example to implement AbstractSequentialList subList() method in Java −

Example

 Live Demo

import java.util.LinkedList;
import java.util.AbstractSequentialList;

public class Demo {
   public static void main(String[] args) {
      AbstractSequentialList<Integer> absSequential = new LinkedList<>();
      absSequential.add(250);
      absSequential.add(320);
      absSequential.add(400);
      absSequential.add(550);
      absSequential.add(600);
      absSequential.add(700);
      absSequential.add(900);
      System.out.println("Elements in the AbstractSequentialList = "+absSequential);
      System.out.println("SubList = " + absSequential.subList(2, 5));
   }
}

Output

Elements in the AbstractSequentialList = [250, 320, 400, 550, 600, 700, 900]
SubList = [400, 550, 600]

Updated on: 30-Jul-2019

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements