Java HashSet clear() 方法

30 Dec 2024 | 2 分钟阅读

Java HashSet 类的 clear() 方法用于从此集合中移除所有元素。

语法

以下是 clear() 方法的声明:

参数

此方法不接受任何参数。

返回值

clear() 方法不返回任何值。

异常

不适用

兼容版本

Java 1.2 及以上版本

示例 1

输出

Hash set Elements: [DFC, SSSIT, JTP]
Hash set elements after clear: []

示例 2

输出

Hash set Elements: [151, 121, 111]
Hash set elements after clear: []

示例 3

输出

Size of the HashSet: 4
Elements of the HashSet before clear:
Google
Twitter
Instagram
Facebook
Elements of the HashSet after clear:
Size of the hashset: 0