获取Java中HashSet的大小
要获取HashSet的大小,请使用size()方法。让我们创建一个HashSet并添加元素-
Set<Integer> hs = new HashSet<Integer>(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91);
现在让我们使用size()并获取HashSet的大小-
hs.size()
以下是获取HashSet大小的示例-
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
Set<Integer> hs = new HashSet<Integer>();
hs.add(15);
hs.add(71);
hs.add(82);
hs.add(89);
hs.add(91);
System.out.println("Elements = "+hs);
System.out.println("Size of Set: "+hs.size());
hs.remove(82);
hs.remove(91);
System.out.println("\nUpdated Elements = "+hs);
System.out.println("Size of Set now: "+hs.size());
}
}输出结果
Elements = [82, 71, 89, 91, 15] Size of Set: 5 Updated Elements = [71, 89, 15] Size of Set now: 3
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短