Home Tutorial Java Core Hashset Java hashset example.

 
 

Java hashset example.
Posted on: April 4, 2011 at 12:00 AM
Java hashset example.

Java hashset example.

    HashSet is a collection. You can not store duplicate value in HashSet.

In this java hashset exmple, you will see how to create HashSet in java application and how to store value in Hashset. There is  a method called add, it is used to store value in it.

Code

CreateHashSet.java

package net.roseindia;

import java.util.HashSet;

public class CreateHashSet {

public static void main(String[] arg) {

HashSet obHashSet = new HashSet();

obHashSet.add("Bharat");

obHashSet.add("Gyan");

obHashSet.add("Ankita");

obHashSet.add("Vrishti");

System.out.println("All data of HashSet : " + obHashSet);

 }  }

Output :
All data of HashSet : [Gyan, Ankita, Vrishti, Bharat]

Related Tags for Java hashset example.: