Home Tutorial Java Core Hashset Example of contains method of hashset in java.

 
 

Share on Google+Share on Google+
Example of contains method of hashset in java.
Posted on: April 4, 2011 at 12:00 AM
Advertisement
Example of contains method of hashset in java.

Example of contains method of hashset in java.

The contains() is a method of hashset. It is used for checking that the given number is available in hashset or not. It returns only boolean value.If number is present in hashset, it returns true otherwise false.

Code

HashSetToArray .java

package net.roseindia;

import java.util.HashSet;

public class HashSetToArray {

public static void main(String[] arg) {

HashSet obHashSet = new HashSet();

obHashSet.add(new Integer(1));

obHashSet.add(new Integer(2));

obHashSet.add(new Integer(3));

obHashSet.add(new Integer(4));

System.out.println("All Elements in HashSet : " + obHashSet);

/*Check element is present or not in Hash set */

boolean b=obHashSet.contains(new Integer(3));

System.out.println("Result : "+ b);

}

}

Output :

All Elements in HashSet : [1, 2, 3, 4]

Result : true

Advertisement

Related Tags for Example of contains method of hashset in java.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 4, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com