How to Remove List From LinkedList

This example shows how to Remove List From LinkedList in Java . This example also shows how  to remove list from linkedlist using removeAll() method .

How to Remove List From LinkedList In Java ?

We can remove list element from linkedlist with the help of removeAll(). LinkedList removeAll() method removes all of this collection’s elements that are also contained in the given collection . Let’s see LinkedList removeAll() method examples .

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]

This is example we have seen how to remove list from LinkedList In Java. You can see more java LinkedList Example .

Leave a Reply

Your email address will not be published. Required fields are marked *

− 8 = 1