Element in HashSet can be removed by remove() method. In remove method, we can pass object to remove.
hashSet.remove(object);
Example of removing element in HashSet
import java.util.HashSet; import java.util.Iterator; public class HashSetRemoveExample { public static void main(String[] args) { HashSet<String> hs=new HashSet<String>(); hs.add("b"); hs.add("a"); hs.add("c"); hs.add("d"); hs.add("d"); // remove hashset object hs.remove("c"); Iterator it=hs.iterator(); while(it.hasNext()) { String value =(String)it.next(); System.out.println("Value :"+value); } } }
Tags: Collections



Link to Us