Java中的HashSet size()方法

爪哇。util。哈希集。size()方法用于获取哈希集的大小或哈希集中存在的元素数。

null

语法:

Hash_Set.size()

参数: 此方法不接受任何参数。

返回值: 该方法返回哈希集中存在的元素的大小或数量。

下面的程序演示了Java。util。哈希集。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

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享