HashSet properties
In this chapter you will learn:
Get the size of a hash set
int size()
Returns the number of elements in this set (its cardinality).
import java.util.HashSet;
// j a v a2 s . 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.size());
hs.remove("A");
System.out.println(hs.size());
}
}
The output:
Is a HashSet empty
boolean isEmpty()
Returns true if this set contains no elements.
import java.util.HashSet;
/*java 2 s. com*/
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.isEmpty());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What is TreeSet
- How to create TreeSet
- Create a TreeSet with custom Comparator
- Create TreeSet from a List
Home » Java Tutorial » Collections