View RSS Feed

Advanced Java

Checking if a Collection Contains a Certain Element

Rate this Entry
by , 02-18-2012 at 05:08 PM (3333 Views)
In collection interface, 2 methods have been implemented to check if certain elements are present in it. Two methods are as following:

1. contains()
2. containsAll()

This code explains these 2 methods:
Java Code: This is the code to explain the contains and containsAll methods
Collection collection   = new HashSet();
boolean containsElement = collection.contains("an element");

Collection elements     = new HashSet();
boolean containsAll     = collection.containsAll(elements);
contains() returns true, when elements are present in collection, & false if not.
containsAll() returns true when contains all the elements are present in the parameter collection & false if not.

Submit "Checking if a Collection Contains a Certain Element" to Facebook Submit "Checking if a Collection Contains a Certain Element" to Digg Submit "Checking if a Collection Contains a Certain Element" to del.icio.us Submit "Checking if a Collection Contains a Certain Element" to StumbleUpon Submit "Checking if a Collection Contains a Certain Element" to Google

Comments