removeAll (Collection c) method of List Interface in java

removeAll (Collection c) method of List Interface in java


Posted in : Core Java Posted on : January 11, 2011 at 6:28 PM Comments : [ 0 ]

In this section we will show you the method of removing all� elements of a specified collection (list) using removeAll(Collection c) method of List Interface in java.

removeAll (Collection c) method of List Interface in java

In this section we will show you the method of removing all elements of a specified collection (list) using removeAll(Collection c) method of List Interface in java. 

Syntax

public boolean removeAll (Collection c)

This method returns a boolean value True/False.

In this method you can remove all elements from a list, it returns either true or false. It returns 'true' when all elements are removed or returns 'false' when list is not removed or does not exist in list. Through this method you can remove all the elements of a specified collection from a list. 

Parameter Description

Collection c : It takes an argument as a collection of elements to be removed from the list. 

Example of removeAll (Collection c) method

In this example we will show you how does removeAll (Collection c) method works in List interface. This example will help you to understand how can you remove all the elements of a list. Through this example we will show you adding of element to the list, addition of two list, size of list,  and deletion of a collection from specified collection.

Example:-


package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
public class ListRemoveAll {
public static void main(String args[]){
ArrayList al =new ArrayList();
ArrayList al2 =new ArrayList();
al.add(1);
al.add(2);
al.add(3);
al.add(4);
al.add(5);
System.out.println("Elements of first list are : "+al);
System.out.println("Size of first list = " +al.size());
al2.add("Suman");
al2.add("Raman");
al2.add("Ranjan");
al2.add("Niranjan");
System.out.println("Elements of second list are : "+al2);
System.out.println("Size of second list = " +al2.size());
boolean b=al.addAll(al2);
System.out.println("After addition of list1 and list2 new list = " );
System.out.println("\t"+al);
System.out.println("Size of list after addition of two lists = " +al.size());
boolean bol=al.removeAll(al2);
System.out.println("List2 is removed : " +bol);
System.out.println("After removing elements of list2 from new list then elements of list1 = ");
System.out.println("\t\t"+al);
System.out.println("Size of list after removing the list2 = " +al.size());
}
}

Output :

Elements of first list are : [1, 2, 3, 4, 5]

Size of first list = 5

Elements of second list are : [Suman, Raman, Ranjan, Niranjan]

Size of second list = 4

After addition of list1 and list2 new list =

[1, 2, 3, 4, 5, Suman, Raman, Ranjan, Niranjan]

Size of list after addition of two lists = 9

List2 is removed : true

After removing elements of list2 from new list then elements of list1 =

[1, 2, 3, 4, 5]

Size of list after removing the list2 = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics