全屏
contains(Object o)方法是用来判断当且仅当此set是否包含指定的元素,方法返回true。
声明
以下是java.util.TreeSet.contains()方法的声明。public boolean contains(Object o)
参数o-- 这是此set要被检查是否包含的对象。
返回值
如果此set包含指定的元素,方法调用返回true。
异常ClassCastException-- 如果指定元素不能与元素相比于当前集合,这个异常被抛出。
NullPointerException-- 如果指定的元素为null,并且此set使用自然顺序,或者其比较器不允许使用null元素,这个异常将被抛出。
例子
下面的例子展示java.util.TreeSet.contains()方法的使用。package cn.sxt;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
// creating a TreeSet
TreeSet treeadd = new TreeSet();
// adding in the tree set
treeadd.add(12);
treeadd.add(13);
treeadd.add(14);
treeadd.add(15);
// check existence of 15
System.out.println("Checking existence of 15 ");
System.out.println("Is 15 there in the set: "+treeadd.contains(15));
}
}
现在编译和运行上面的代码示例,将产生以下结果。Checking existence of 15
Is 15 there in the set: true
分享到:
0评论