目录

boolean contains(Object o)

描述 (Description)

当且仅当此set包含指定元素时, contains(Object o)方法用于返回true。

声明 (Declaration)

以下是java.util.TreeSet.contains()方法的声明。

public boolean contains(Object o)

参数 (Parameters)

o - 这是在此集合中检查包含的对象。

返回值 (Return Value)

如果此set包含指定的元素,则方法调用返回true。

异常 (Exception)

  • ClassCastException - 如果无法将指定的元素与集合中当前存在的元素进行比较,则抛出此异常。

  • NullPointerException - 如果指定的元素为null并且此set使用自然排序,或者其比较器不允许null元素,则抛出此异常。

例子 (Example)

以下示例显示了java.util.TreeSet.contains()方法的用法。

package com.iowiki;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
   public static void main(String[] args) {
      // creating a TreeSet 
      TreeSet <Integer>treeadd = new TreeSet<Integer>();
      // 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
↑回到顶部↑
WIKI教程 @2018