• Java Data Structures Tutorial

Remove elements from a Set



The remove() method of the set interface accepts an element as parameter and deletes it from the current set collection.

You can remove an element from a set using this method.

Example

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

public class RemovingElements {
   public static void main(String args[]) {
      Set set = new HashSet();      
      set.add(100);
      set.add(501);
      set.add(302);
      set.add(420);
      System.out.println("Contents of the set are: "+set);

      set.remove(100);
      System.out.println("Contents of the set after removing one element : "+set);
   }
}

Output

Contents of the set are: [100, 420, 501, 302]
Contents of the set after removing one element : [420, 501, 302]
Advertisements