Java中的HashSet size()方法
Java.util.HashSet.size()方法用于获取HashSet的大小或HashSet中的元素数量。
语法:
Hash_Set.size()
参数: 此方法不需要任何参数。
返回值: 该方法返回HashSet中的大小或元素数量。
下面的程序说明了Java.util.HashSet.size()方法:
// Java code to illustrate HashSet.size() method
import java.util.*;
import java.util.HashSet;
public class HashSetDemo {
public static void main(String args[])
{
// Creating an empty HashSet
HashSet<String> set = new HashSet<String>();
// Use add() method to add elements into the Set
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("4");
set.add("Geeks");
// Displaying the HashSet
System.out.println("HashSet: " + set);
// Displaying the size of the HashSet
System.out.println("The size of the set is: " + set.size());
}
}
HashSet: [4, Geeks, Welcome, To]
The size of the set is: 4