12:58 pm - Wednesday February 22, 2017

Java Program used to retain only elements specified in the specified collection using retainAll() method.

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

RetailAll program to retain only elements specified in the specified collection.

Example Program

import java.util.ArrayList;
import java.util.LinkedList;

public class RetainAll {
	public static void main(String[] args) {
		LinkedList<String> ll = new LinkedList<String>();
		ll.add("gowtham");
		ll.add("narendar");
		ll.add("saravanan");
		ll.add("kumar");
		ArrayList<String> al = new ArrayList<String>();
		al.add("gowtham");
		al.add("narendar");
		al.add("ram");
		System.out.println("linkedlist" + ll);
		System.out.println("Arraylist" + al);
		ll.retainAll(al);
		System.out.println("linkedlist after using retainall" + ll);
		System.out.println("arraylist after using retainall" + al);

	}

}

Output

linkedlist[gowtham, narendar, saravanan, kumar]

Arraylist[gowtham, narendar, ram]

linkedlist after using retainall[gowtham, narendar]

arraylist after using retainall[gowtham, narendar, ram]

Description

The retainAll(Collection<?> c) method is used to retain only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection.

Declaration

Following is the declaration for java.util.Vector.retainAll() method

public boolean retainAll(Collection<?> c)

Parameters

c–This is the collection of elements to be retained in this Vector.

Return Value

The method call returns true if this Vector is changed as a result of the call.

Exception

    NullPointerException–This exception is thrown if the specified collection is null.

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.