java.util.HashSet.contains() 方法
描述
contains(Object o) 方法用于在此集合包含指定元素时返回"true"。
声明
以下是 java.util.HashSet.contains() 方法的声明。
public boolean contains(Object o)
参数
o − 这是要测试其存在于该集合中的元素。
返回值
如果此集合包含指定元素,则方法调用返回"true"。
异常
NA
示例
下面的例子展示了 java.util.HashSet.contains() 的用法。
package com.tutorialspoint;
import java.util.*;
public class HashSetDemo {
public static void main(String args[]) {
// create hash set
HashSet <String> newset = new HashSet <String>();
// populate hash set
newset.add("Learning");
newset.add("Easy");
newset.add("Simply");
// check the existence of element
boolean exist = newset.contains("Simply");
System.out.println("Is the element 'Simply' exists: "+ exist);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Is the element 'Simply' exists: true

