Remove element from HashSet : HashSet « Collections Data Structure « Java

Remove element from HashSet


import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetExample {
  public static void main(String[] args) {
    Set<Integer> set = new HashSet<Integer>();

    set.add(new Integer(1));
    set.add(new Integer(2));
    set.add(new Integer(3));
    set.add(new Integer(4));
    set.add(new Integer(5));
    set.add(new Integer(6));
    set.add(new Integer(7));
    set.add(new Integer(8));
    set.add(new Integer(9));
    set.add(new Integer(10));

    // Use iterator to display the vsetes
    System.out.println("HashSet Before: ");
    for (Iterator i = set.iterator(); i.hasNext();) {
      Integer integer = (Integer);

    // Remove the integer 6
    System.out.println("\nRemove integer 6");
    set.remove(new Integer(6));

    // Use iterator to display the vsetes
    System.out.println("\nHashSet After: ");
    for (Iterator i = set.iterator(); i.hasNext();) {
      Integer integer = (Integer);



Related examples in the same category

1.Add values to HashSet
2.HashSet implementation of setHashSet implementation of set
3.Generic collection conversion: HashSet and ArrayList
4.Remove one set from another set
5.Find maximum element of Java HashSet
6.Find Minimum element of Java HashSet
7.Get Enumeration over Java HashSet
8.Get Synchronized Set from Java HashSet
9.Check if a particular element exists in Java HashSet
10.Copy all elements of Java HashSet to an Object Array
11.Get Size of Java HashSet
12.Iterate through elements of Java HashSet
13.Integer value set
14.Listing the Elements of a Collection(iterate over the elements of set or list)
15.Remove specified element from Java HashSet
16.Remove all elements from Java HashSet
17.Convert array to Set
18.Implements a HashSet where the objects given are stored in weak references
19.Convert an ArrayList to HashSet
20.Create an array containing the elements in a set
21.Duplicate elements are discarded
22.Convert Set into array
23.A memory-efficient hash set.
24.Compact HashSet
25.Coarse-grained hash set.
26.Concurrent hash set that allows the lock array to be resized.
27.A CompactHashSet implements the set interface more tightly in memory and more efficiently than Java's java.util.HashSet.
28.Concurrent HashSet