- HashSet is a cast of collection framework which extends AbstractSet cast in addition to implements the Set interface.
- HasSet doesn't guarantee that elements guild volition stay same over the fourth dimension in addition to returned inwards whatever random order.
- HasSet doesn't permit duplicate values. If you lot endeavour to insert duplicate, It volition overwrite.
- HasSet allows to shop nothing values.
- HasSet implementation is non synchronized.
Important Methods of HashSet
- boolean add(E e) : To add together elements inwards 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 provide truthful if given values is introduce inwards HashSet.
- boolean isEmpty() : It volition provide truthful if HasSet is empty.
- Iterator<E> iterator() : It volition returns an iterator over the elements inwards Set.
- boolean remove(Object o) : It volition take specified elements from laid if it is available inwards HashSet.
- int size() : It volition provide size of HashSet.
Bellow given illustration volition demo you lot utilise of unlike HashSet methods.
package JAVAExamples; import java.util.HashSet; populace cast HashSetJavaExample { populace static void main(String args[]) { HashSet hset = novel HashSet(); //Check if HashSet is empty. System.out.println("HashSet is empty? : "+hset.isEmpty()); //Add elements inwards HashSet. hset.add("One"); hset.add("Two"); hset.add("Three"); hset.add(null); //HassSet allows nothing 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 cistron 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 post away role HashSet to shop values inwards random guild including null.