HashSet Class
A HashSet represents a set of elements.it does not guarantee the order of elements.Also it does not allow the duplicate elements to be stored.
Default Initial Capacity = 16
Default Load Factor = 0.75
HashSet Class Methods:
boolean add(obj)
boolean remove(obj)
void clear()
boolean contains(obj)
boolean isEmpty()
int size()
Program:
/**
*
*/
package com.collectionpack;
import java.util.HashSet;
import java.util.Iterator;
/**
* @author Abhinaw.Tripathi
*
*/
public class HashSetApp {
/**
* @param args
*/
public static void main(String[] args) {
HashSet<String> hs=new HashSet<>();
hs.add("India");
hs.add("America");
hs.add("Japan");
hs.add("China");
System.out.println("HashSet =" + hs);
Iterator it=hs.iterator();
System.out.println("Elements using Iterator....");
while(it.hasNext())
{
String s=(String) it.next();
System.out.println(" "+s);
}
}
}
Output:
HashSet =[America, China, Japan, India]
Elements using Iterator....
America
China
Japan
India