- Java.util 包類
- Java.util - 首頁
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他內容
- Java.util - 介面
- Java.util - 異常
- Java.util - 列舉
- Java.util 有用資源
- Java.util - 有用資源
- Java.util - 討論
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
廣告