boolean contains(Object o)
描述 (Description)
如果此set包含指定的元素,则contains(Object o)方法用于返回'true'。
声明 (Declaration)
以下是java.util.HashSet.contains()方法的声明。
public boolean contains(Object o)
参数 (Parameters)
o - 这是要测试其在该集合中的存在的元素。
返回值 (Return Value)
如果此set包含指定的元素,则方法调用返回'true'。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.util.HashSet.contains()的用法
package com.iowiki;
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