Java HashSet contains() 方法

2024 年 12 月 18 日 | 2 分钟阅读

Java HashSet 类的 contains() 方法用于检查此 HashSet 是否包含指定的元素。如果找到该元素,则返回 true,否则返回 false。

语法

以下是 contains() 方法的声明

参数

参数描述必需/可选
o这是要测试其在此集合中是否存在/的元素。必需

返回值

如果此集合包含指定的元素,则 contains() 方法返回 true。

异常

不适用

兼容版本

Java 1.2 及以上版本

示例 1

输出

Hash set Elements: [21, 151, 11, 110, 15]
Does the Set contains '110'? :- true
Does the Set contains '555'? :- false

示例 2

输出

Enter student name: Rishi
Rishi found on student list.

示例 3

输出

Elements of the HashSet:
Twitter
Instagram
Facebook
Does the HashSet contains 'Whatsapp'? :- false
Does the HashSet contains 'Facebook'? :- true