Checking if a Collection Contains a Certain Element
by, 02-18-2012 at 05:08 PM (2674 Views)
In collection interface, 2 methods have been implemented to check if certain elements are present in it. Two methods are as following:
This code explains these 2 methods:
contains() returns true, when elements are present in collection, & false if not.Java Code: This is the code to explain the contains and containsAll methodsCollection collection = new HashSet(); boolean containsElement = collection.contains("an element"); Collection elements = new HashSet(); boolean containsAll = collection.containsAll(elements);
containsAll() returns true when contains all the elements are present in the parameter collection & false if not.