retainAll (Collection c) method of List Interface in java.

retainAll (Collection c) method of List Interface in java.


Posted in : Core Java Posted on : January 12, 2011 at 5:17 PM Comments : [ 0 ]

In this section we will discuss about the method of retaining only the element of current list which are also contained by the specified list.

retainAll (Collection c) method of List Interface in java.

In this section we will discuss about the method of retaining only the element of current list which are also contained by the specified list.

Syntax

public boolean retainAll (Collection c)

This method returns a boolean value True/False.

This method is used to retain the elements of one list into another. In other word we can say, it deletes all those elements of current list collection which do not exist in specified collection object. It copies those elements which are equal in both list collection objects and deletes rest of the other elements from the specified list. This method returns true if the list has changed after the call, otherwise false.

Parameter Description

Collection c : It takes an argument as List Object (collection) for which collection's elements do you want to retain.

Example of retainAll (Collection c) method

In this example we will show you how does retainAll (Collection c) method works in List interface. This example will help you to understand how can you retain the element of current list which are also contained by the specified list. Through this example we will show you size of list, and deletion of elements from specified  collection.

Example:-


package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class ListRetainAll {
public static void main(String[] args) {
// Create two collections:
ArrayList al = new ArrayList();
LinkedList ll = new LinkedList();
ll.add("1");
ll.add("2");
ll.add("3");
ll.add("4");
System.out.println("The LinkedList collection before: " + ll);
System.out.println("Size of LinkedList :"+ll.size());
al.add("1");
al.add("2");
al.add("3");
al.add("4");
al.add("5");
al.add("6");
System.out.println("The ArrayList collection before : " + al);
System.out.println("Size of ArrayList :"+al.size());
// Delete the elements of ArrayList that are not in LinkedList:
boolean b=al.retainAll(ll);
//If list get changed return true otherwise false
System.out.println("List becomes change :"+b);
// Display the collection after:
System.out.println("The ArrayList collection after : " + al);
System.out.println("Size of ArrayList remains :"+al.size());
System.out.println("The LinkedList collection after: " + ll);
System.out.println("Size of LinkedList :"+ll.size());
}
}

Output :

The LinkedList collection before: [1, 2, 3, 4]

Size of LinkedList : 4

The ArrayList collection before : [1, 2, 3, 4, 5, 6]

Size of ArrayList : 6

List becomes change : true

The ArrayList collection after : [1, 2, 3, 4]

Size of ArrayList remains : 4

The LinkedList collection after: [1, 2, 3, 4]

Size of LinkedList : 4

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics