java.util.HashSet.size() 方法
描述
size() 方法用于获取该集合中的元素个数。
声明
以下是 java.util.HashSet.size() 方法的声明。
public int size()
参数
NA
返回值
方法调用返回此集合中的元素个数。
异常
NA
示例
下面的例子展示了 java.util.HashSet.size() 的用法。
package com.tutorialspoint;
import java.util.*;
public class HashSetDemo {
public static void main(String args[]) {
// create hash set
HashSet <String> newset = new HashSet <String>();
// populate hash set
newset.add("Learning");
newset.add("Easy");
newset.add("Simply");
System.out.println("Size of the set: "+newset.size());
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Size of the set: 3

