- HashSet is a course of education of collection framework which extends AbstractSet course of education in addition to implements the Set interface.
- HasSet doesn't guarantee that elements social club volition stay same over the fourth dimension in addition to returned inward whatever random order.
- HasSet doesn't let duplicate values. If yous essay to insert duplicate, It volition overwrite.
- HasSet allows to shop nil values.
- HasSet implementation is non synchronized.
Important Methods of HashSet
- boolean add(E e) : To add together elements inward to laid if it is non already present.
- void clear() : Remove all entries from set.
- Object clone() : It volition returns shallow re-create of HasSet instance.
- boolean contains(Object o) : It volition supply truthful if given values is introduce inward HashSet.
- boolean isEmpty() : It volition supply truthful if HasSet is empty.
- Iterator<E> iterator() : It volition returns an iterator over the elements inward Set.
- boolean remove(Object o) : It volition withdraw specified elements from laid if it is available inward HashSet.
- int size() : It volition supply size of HashSet.
Bellow given representative volition present yous utilization of unlike HashSet methods.
package JAVAExamples; import java.util.HashSet; world course of education HashSetJavaExample { world static void main(String args[]) { HashSet hset = novel HashSet(); //Check if HashSet is empty. System.out.println("HashSet is empty? : "+hset.isEmpty()); //Add elements inward HashSet. hset.add("One"); hset.add("Two"); hset.add("Three"); hset.add(null); //HassSet allows nil values. hset.add("Four"); //Print HashSet. System.out.println("HashSet elements are : "+hset); //Check HashSet size. System.out.println("Size of HashSet is : "+hset.size()); //Removing chemical ingredient from HashSet. hset.remove("Two"); System.out.println("Now HashSet elements are : "+hset); } }
Output :
HashSet is empty? : truthful HashSet elements are : [null, One, Four, Two, Three] Size of HashSet is : five Now HashSet elements are : [null, One, Four, Three]
This way, You tin rank the sack purpose HashSet to shop values inward random social club including null.