Get SubList from LinkedList - Java Collection Framework

Java examples for Collection Framework:LinkedList

Description

Get SubList from LinkedList

Demo Code

 
import java.util.LinkedList;
import java.util.List;
 
public class Main {
 
  public static void main(String[] args) {
   //from ww w.j av a  2 s.c o m
    //create LinkedList object
    LinkedList lList = new LinkedList();
   
    //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);
   
    List lst = lList.subList(1,4);
    System.out.println("Sublist contains : " + lst);
 
     //remove element from sublist
     lst.remove(2);
     System.out.println("Sublist now contains : " + lst);
     System.out.println("Original LinkedList now contains : " + lList);
  }
}

Result


Related Tutorials