Collections

how to create a HashSet using Collections in java
Previous Home Next

 



 


 
package r4r.co.in;
package r4r.co.in;
import java.util.*;


public class CollectionExample
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
HashSet<Integer> set=new HashSet<Integer>(); // creating a HashSet named set
set.add(1); // adding elements to the set
set.add(3);
set.add(4);
set.add(5);
set.add(7);
set.add(1);
set.add(4);
set.add(7);
System.out.println("The contents of the set are:");
System.out.println(set); // displaying the contents of the set

}


}

The output of the above written code is as follows:

The contents of the set are:
[1, 3, 4, 5, 7]
Previous Home Next