
- 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 add() 方法
描述
Java TreeSet add(Object o) 方法用於向此集合新增指定的元素(如果它還不存在)。
宣告
以下是 java.util.TreeSet.add() 方法的宣告。
public boolean add(Object o)
引數
o − 這是要新增到此集合的元素。
返回值
如果集合中還不包含指定的元素,則方法呼叫返回 true。
異常
ClassCastException − 如果指定的物件無法與集合中當前的元素進行比較,則丟擲此異常。
向 TreeSet 新增條目示例
以下示例演示瞭如何使用 Java TreeSet add() 方法向 TreeSet 新增條目。我們建立了一個 Integer 型別的 TreeSet 物件。然後使用 add() 方法新增一些條目,並列印 TreeSet 物件以檢查其內容。
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); // displaying the Tree set data System.out.print("Tree set : " + treeset); } }
輸出
讓我們編譯並執行上面的程式,這將產生以下結果。
Tree set : [12, 13, 14, 15]
向 TreeSet 新增條目示例
以下示例演示瞭如何使用 Java TreeSet add() 方法向 TreeSet 新增條目。我們建立了一個 String 型別的 TreeSet 物件。然後使用 add() 方法新增一些條目,並列印 TreeSet 物件以檢查其內容。
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"); // displaying the Tree set data System.out.print("Tree set : " + treeset); } }
輸出
讓我們編譯並執行上面的程式,這將產生以下結果。
Tree set : [12, 13, 14, 15]
向 TreeSet
以下示例演示瞭如何使用 Java TreeSet add() 方法向 TreeSet 新增條目。我們建立了一個 Student 物件型別的 TreeSet 物件。然後使用 add() 方法新增一些條目,並列印 TreeSet 物件以檢查其內容。
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(new Student(1, "Robert")); treeset.add(new Student(2, "Julie")); treeset.add(new Student(3, "Adam")); treeset.add(new Student(4, "Julia")); // displaying the Tree set data System.out.print("Tree set : " + treeset); } } 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; } }
輸出
讓我們編譯並執行上面的程式,這將產生以下結果。
Tree set : [[ 1, Robert ], [ 2, Julie ], [ 3, Adam ], [ 4, Julia ]]
java_util_treeset.htm
廣告