Monday 18 May 2015

How to Remove List From LinkedList

 We can remove list element from linkedlist with the help of removeAll().

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

public class RemoveList {


    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 list Linklist is = "+linkedList);
       
        List<String> arr = new LinkedList<String>();
        arr.add("Delhi");
        arr.add("noida");
        arr.add("sre");
       
        /*
         * Removes all of this collection's elements that
         *  are also contained in the specified collection
         */
   
        linkedList.removeAll(arr);
       
        System.out.println("After Remove list Linklist is = "+linkedList);
       
    }
   
}

Output:

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

After Remove list Linklist is = [Mumbai, Noida, Gao, Patna]


No comments:

Post a Comment