Collection Interface remove() Method

Collection Interface remove() Method


Posted in : Java Posted on : November 24, 2010 at 6:56 PM Comments : [ 0 ]

This method used to remove one instance of object from the invoking collection. It returns true if the element was removed or it returns false if the element not remove from the invoking collection

Collection Interface remove() Method:

This method used to remove one instance of object from the invoking collection. It returns true if the element was removed or it returns false if the element not remove from the invoking collection.

Syntax :

boolean remove(Object ob);

Example :

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

public class CollectionInterfaceRemoveMethodExample {
 public static void main(String[] args) {
   Collection collection = new ArrayList();
   collection.add("Ankit");
   collection.add("Deepak");
   collection.add("Ravi");
   collection.add("Avanish");
   System.out.print(" Elements Of the Array List before remove are ");
   System.out.println(collection + ".");
   boolean result = collection.remove("Deepak");
   if(result==true){
	System.out.print(" Elements Of the Array List after remove are ");
	System.out.print(collection + ".");  
   }
  else{
   System.out.print("Element not remove form Array List");
  }	      	      
 }
}

In this example we have create a ArrayList object and add elements using add() method and remove element using remove() method and check result on the base of remove method return value.

Output:

Elements Of the Array List before remove are [Ankit, Deepak, Ravi, Avanish].
Elements Of the Array List after remove are [Ankit, Ravi, Avanish].

Download This example code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics