Java中的HashSet size()方法

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程