HashSet element removing and clearing
In this chapter you will learn:
Clear a hash set
void clear()
Removes all of the elements from this set.
import java.util.HashSet;
// j a v a2s. co m
public class Main{
public static void main(String args[]) {
HashSet<String> hs = new HashSet<String>();
hs.add("java2s.com");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("j a v a2s.com");
System.out.println(hs);
hs.clear();
System.out.println(hs);
}
}
Remove an element from hash set
boolean remove(Object o)
Removes the specified element from this set if it is present.
import java.util.HashSet;
/*from j a va 2 s. c om*/
public class Main {
public static void main(String args[]) {
HashSet<String> hs = new HashSet<String>();
hs.add("java2s.com");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("j a v a2s.com");
System.out.println(hs);
hs.remove("A");
System.out.println(hs);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Collections