How to Remove List From Arraylist

In this post we will see how to remove list of objects from another list in java . For this we will use arraylist.removeAll() method . In earlier i have explain removeAll() Method(). There we will see how to remove sublist from arraylist in java

import java.util.ArrayList;
import java.util.List;

public class RemoveList {

public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
// add element in arraylist
arr.add("c");
arr.add("php");
arr.add("html");
arr.add("java");
// print arraylist
System.out.println(" berfore remove arrayList is = " + arr);

List<String> brr = new ArrayList<String>();
brr.add("php");
brr.add("java");

/*
* Removes from this list all of its elements that are
contained in the specified collection.
*/

arr.removeAll(brr);

System.out.println("after remove arraylist is = "+arr);

}

}

Output:

berfore remove arrayList is = [c, php, html, java]

after remove arraylist is = [c, html]

 

Leave a Reply

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

31 − 24 =