Remove element from Set
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
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));
System.out.println("HashSet Before: ");
for (Iterator i = set.iterator(); i.hasNext();) {
Integer integer = (Integer) i.next();
System.out.println(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) i.next();
System.out.println(integer);
}
}
}
Output:
HashSet Before:
1
2
3
4
5
6
7
8
9
10
Remove integer 6
HashSet After:
1
2
3
4
5
7
8
9
10
Home
Java Book
Runnable examples
Java Book
Runnable examples
Collection Set:
- Convert set to an Array
- Convert Set into List
- Convert a set to a synchronized Set
- Is a particular value in(exist) Set
- Iterator a set
- Maximum element of Set
- Minimum element of a Set
- Remove all elements from a set
- Remove element from Set
- Remove one set from another set
- Remove all the elements from one set to another set
- Union(Add) all the elements from one set to another set
- Intersection of one set and another set
- Set operations: union, intersection, difference
- Set operations: symmetric difference, is subset, is superset
- Size of a Set