This Java Example shows how to remove specific elements which exists in HashSet.
remove in Java HashSet Example
This Java Example shows how to remove specific elements which exists in HashSet. To remove elements exists in HashSetWe use remove() method of HashSet class. It removes the specified element stored in the specified object within the collection.
Example:- RemoveInHashSet.java
package devmanuals.com; import java.util.*; public class RemoveInHashSet { public static void main(String[] args) { HashSet hst = new HashSet(); hst.add("Gyan"); hst.add("Rohit"); hst.add("Anand"); hst.add("Arunesh"); System.out.println("The Elements are: " + hst); boolean bl = hst.remove("Gyan"); boolean b2 = hst.contains("Gyan"); System.out.println("Is Gyan exists? :" + b2); System.out.println("The Elements are: " + hst); } }
Output:-
The Elements are: [Rohit, Gyan, Arunesh, Anand]
Is Gyan exists? : false The Elements are: [Rohit, Arunesh, Anand] |
[ 0 ] Comments