
- 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 TreeMap values() 方法
描述
Java TreeMap values() 方法用於返回此對映中包含的值的集合檢視。集合的迭代器按對應鍵的升序返回值。
宣告
以下是 java.util.TreeMap.values() 方法的宣告。
public Collection<V> values()
引數
無
返回值
方法呼叫返回此對映中包含的值的集合檢視。
異常
無
獲取整數、整數對TreeMap的值的集合示例
以下示例演示瞭如何使用 Java TreeMap values() 方法獲取對映中存在的值的集合檢視。我們建立了一個 Integer,Integer 對的 TreeMap 物件。然後添加了一些條目,並使用 values() 按升序列印值。
package com.tutorialspoint; import java.util.String; public class TreeMapDemo { public static void main(String[] args) { // creating maps TreeMap<Integer, Integer> treemap = new TreeMap<>(); SortedMap<Integer, Integer> treemapincl = new TreeMap<>(); // populating tree map treemap.put(2, 2); treemap.put(1, 1); treemap.put(3, 3); treemap.put(6, 6); treemap.put(5, 5); System.out.println("Getting collection from the map"); Collection coll = treemap.values(); System.out.println("Value of the collection: "+coll); } }
輸出
讓我們編譯並執行上述程式,這將產生以下結果。
Getting collection from the map Value of the collection: [1, 2, 3, 4, 5]
獲取整數、字串對TreeMap的值的集合示例
以下示例演示瞭如何使用 Java TreeMap values() 方法獲取對映中存在的值的集合檢視。我們建立了一個 Integer,String 對的 TreeMap 物件。然後添加了一些條目,並使用 values() 按升序列印值。
package com.tutorialspoint; import java.util.String; public class TreeMapDemo { public static void main(String[] args) { // creating maps TreeMap<Integer, String> treemap = new TreeMap<>(); SortedMap<Integer, String> treemapincl = new TreeMap<>(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); System.out.println("Getting collection from the map"); Collection coll = treemap.values(); System.out.println("Value of the collection: "+coll); } }
輸出
讓我們編譯並執行上述程式,這將產生以下結果。
Getting collection from the map Value of the collection: [one, two, three, five, six]
獲取整數、物件對TreeMap的值的集合示例
以下示例演示瞭如何使用 Java TreeMap values() 方法獲取對映中存在的值的集合檢視。我們建立了一個 Integer,Student 對的 TreeMap 物件。然後添加了一些條目,並使用 values() 按升序列印值。
package com.tutorialspoint; import java.util.String; public class TreeMapDemo { public static void main(String[] args) { // creating maps TreeMap<Integer, Student> treemap = new TreeMap<>(); SortedMap<Integer, Student> treemapincl = new TreeMap<>(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); System.out.println("Getting collection from the map"); Collection coll = treemap.values(); System.out.println("Value of the collection: "+coll); } } class 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) { if(obj == null) return false; Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
輸出
讓我們編譯並執行上述程式,這將產生以下結果。
Getting collection from the map Value of the collection: [one, two, three, five, six]
java_util_treemap.htm
廣告