Java TreeSet contains() 方法



描述

Java TreeSet contains(Object o) 方法用於返回 true,當且僅當此集合包含指定的元素。

宣告

以下是 java.util.TreeSet.contains() 方法的宣告。

public boolean contains(Object o)

引數

o − 要檢查此集合中是否包含的物件。

返回值

如果此集合包含指定的元素,則方法呼叫返回 true。

異常

  • ClassCastException − 如果指定的元素無法與集合中當前存在的元素進行比較,則丟擲此異常。

  • NullPointerException − 如果指定的元素為 null 且此集合使用自然排序,或者其比較器不允許 null 元素,則丟擲此異常。

在 Integer 型別 TreeSet 中檢查元素是否存在示例

以下示例演示瞭如何使用 Java TreeSet contains() 方法來檢查元素是否出現在 TreeSet 中。我們建立了一個 Integer 型別的 TreeSet 物件。然後使用 add() 方法添加了一些條目,並在集合中檢查一個元素,並列印結果。

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating a TreeSet 
      TreeSet <Integer>treeset = new TreeSet<>();

      // adding in the tree set
      treeset.add(12);
      treeset.add(13);
      treeset.add(14);
      treeset.add(15);

      // check existence of 15  
      System.out.println("Checking existence of 15 ");
      System.out.println("Is 15 there in the set: "+treeset.contains(15));
   }    
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Checking existence of 15 
Is 15 there in the set: true

在 String 型別 TreeSet 中檢查元素是否存在示例

以下示例演示瞭如何使用 Java TreeSet contains() 方法來檢查元素是否出現在 TreeSet 中。我們建立了一個 String 型別的 TreeSet 物件。然後使用 add() 方法添加了一些條目,並在集合中檢查一個元素,並列印結果。

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating a TreeSet 
      TreeSet <String>treeset = new TreeSet<>();

      // adding in the tree set
      treeset.add("12");
      treeset.add("13");
      treeset.add("14");
      treeset.add("15");

      // check existence of "15"  
      System.out.println("Checking existence of 15 ");
      System.out.println("Is 15 there in the set: "+treeset.contains("15"));
   }    
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Checking existence of 15 
Is 15 there in the set: true

在 Object 型別 TreeSet 中檢查元素是否存在示例

以下示例演示瞭如何使用 Java TreeSet contains() 方法來檢查元素是否出現在 TreeSet 中。我們建立了一個 Student 物件型別的 TreeSet 物件。然後使用 add() 方法添加了一些條目,並在集合中檢查一個元素,並列印結果。

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating a TreeSet 
      TreeSet <Student>treeset = new TreeSet<>();

      // adding in the tree set
      treeset.add(new Student(1, "Robert"));
      treeset.add(new Student(2, "Julie"));
      treeset.add(new Student(3, "Adam"));
      treeset.add(new Student(4, "Julia"));

      // check existence of "Adam"  
      System.out.println("Checking existence of 15 ");
      System.out.println("Is Adam there in the set: "+treeset.contains(new Student(3, "Adam")));
   }    
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Checking existence of 15 
Is Adam there in the set: true
java_util_treeset.htm
廣告

© . All rights reserved.