Collection Interface contains () Method

Collection Interface contains () Method


Posted in : Java Posted on : November 25, 2010 at 11:11 AM Comments : [ 0 ]

Using this method we can check whether the element exist or not in the invoking collection.

Collection Interface contains() Method:

Using this method we can check whether the element exist or not in the invoking collection. It returns true an element exist in the invoking collection. Otherwise, returns false.

Syntax :

boolean contains(Object ob);

Example :

import java.util.*;

public class CollectionInterfaceContainsMethodExample {

 public static void main(String[] args) {
  Collection collection = new ArrayList();
     
	collection.add(1);
	collection.add(2);
	collection.add(3);
			
	boolean result1 = collection.contains(1);
	if(result1==true){
		System.out.println("Element 1 exist in the invoking collection");			
	}
	else{
		System.out.println("Element 1 does not exist in the invoking collection");
	}
	
	boolean result2 = collection.contains(5);
	if(result2==true){
		System.out.print("Element 5 exist in the invoking collection");			
	}
	else{
		System.out.print("Element 5 does not exist in the invoking collection");
	}
 }
}

Output :

Element 1 exist in the invoking collection
Element 5 does not exist in the invoking collection

Download This example code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics