Remove range of elements from LinkedList - Java Collection Framework

Java examples for Collection Framework:LinkedList

Description

Remove range of elements from LinkedList

Demo Code


import java.util.LinkedList;
import java.util.List;
 
public class Main {
 
  public static void main(String[] args) {
 
    //create LinkedList object
    LinkedList lList = new LinkedList();
   //  ww w.j  av a2 s.  c  om
    //add elements to LinkedList
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
   
    System.out.println("LinkedList contains : " + lList);
   
    //remove elements from index 2(inclusive) to 5(exclusive)
    lList.subList(2,5).clear();
   
    System.out.println("Range of elements removed from LinkedList");
    System.out.println("LinkedList now contains : " + lList);
  }
}

Result


Related Tutorials