Monday, 18 May 2015

How to get sub list from LinkedList

import java.util.LinkedList;
import java.util.List;

public class GetSubList {

    public static void main(String[] args) {
       
        LinkedList<String> linkedList = new LinkedList<String>();
       
        linkedList.add("Mumbai");
        linkedList.add("Delhi");
        linkedList.add("Noida");
        linkedList.add("Gao");
        linkedList.add("Patna");
       
        //print linkedlist
        System.out.println("Before Remove Element Linklist is = "+linkedList);
       
        /*
         * Returns a view of the portion of this list between the
           specified fromIndex, inclusive, and toIndex, exclusive.
         */
       
        List<String> list = linkedList.subList(2, 4);
       
        System.out.println("Sublist is ="+list);
       
    }
   
}

Output:

Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

Sublist is =[Noida, Gao]

No comments:

Post a Comment