
- 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 ListResourceBundle handleKeySet() 方法
描述
Java ListResourceBundle handleKeySet() 方法返回僅包含在此 ResourceBundle 中的鍵的集合。
宣告
以下是 java.util.ListResourceBundle.handleKeySet() 方法的宣告
protected Set<String> handleKeySet()
引數
無
返回值
此方法返回僅包含在此 ResourceBundle 中的鍵的集合。
異常
無
獲取字串、整數對 ListResourceBundle 的鍵集示例
以下示例演示了 Java ListResourceBundle handleKeySet() 方法的使用,用於迭代 ListResourceBundle 的鍵。我們建立了一個 ListResourceBundle 物件,其內容為字串、整數對的二維陣列。然後透過重寫 getContents() 方法新增一些條目,然後在重寫的 handleKeySet() 方法中檢索鍵的列舉並進行迭代以獲取鍵。在主方法中,我們使用 handleKeySet() 方法獲取鍵的集合。
package com.tutorialspoint; import java.util.Enumeration; import java.util.HashSet; import java.util.ListResourceBundle; import java.util.Set; // create a class that extends to ListResourceBundle class MyResources extends ListResourceBundle { // get contents must be implemented protected Object[][] getContents() { return new Object[][] { {"1", 1}, {"2", 2}, {"3", 3} }; } protected Set<String> handleKeySet() { Set<String> set = new HashSet<String>(); // get the keys Enumeration<String> enumeration = getKeys(); // print the keys while (enumeration.hasMoreElements()) { set.add(enumeration.nextElement()); } return set; } } public class ListResourceBundleDemo { public static void main(String[] args) { // create a new MyResources instance MyResources mr = new MyResources(); // get the set of Keys Set<String> keys = mr.handleKeySet(); // print each key keys.forEach(System.out::println); } }
輸出
讓我們編譯並執行上述程式,這將產生以下結果。
1 2 3
獲取字串、字串對 ListResourceBundle 的鍵集示例
以下示例演示了 Java ListResourceBundle handleKeySet() 方法的使用,用於迭代 ListResourceBundle 的鍵。我們建立了一個 ListResourceBundle 物件,其內容為字串、字串對的二維陣列。然後透過重寫 getContents() 方法新增一些條目,然後在重寫的 handleKeySet() 方法中檢索鍵的列舉並進行迭代以獲取鍵。在主方法中,我們使用 handleKeySet() 方法獲取鍵的集合。
package com.tutorialspoint; import java.util.Enumeration; import java.util.HashSet; import java.util.ListResourceBundle; import java.util.Set; // create a class that extends to ListResourceBundle class MyResources extends ListResourceBundle { // get contents must be implemented protected Object[][] getContents() { return new Object[][] { {"1", "Hello World!"}, {"2", "Goodbye World!"}, {"3", "Goodnight World!"} }; } protected Set<String> handleKeySet() { Set<String> set = new HashSet<String>(); // get the keys Enumeration<String> enumeration = getKeys(); // print the keys while (enumeration.hasMoreElements()) { set.add(enumeration.nextElement()); } return set; } } public class ListResourceBundleDemo { public static void main(String[] args) { // create a new MyResources instance MyResources mr = new MyResources(); // get the set of Keys Set<String> keys = mr.handleKeySet(); // print each key keys.forEach(System.out::println); } }
輸出
讓我們編譯並執行上述程式,這將產生以下結果。
1 2 3
獲取字串、物件對 ListResourceBundle 的鍵集示例
以下示例演示了 Java ListResourceBundle handleKeySet() 方法的使用,用於迭代 ListResourceBundle 的鍵。我們建立了一個 ListResourceBundle 物件,其內容為字串、Student 物件對的二維陣列。然後透過重寫 getContents() 方法新增一些條目,然後在重寫的 handleKeySet() 方法中檢索鍵的列舉並進行迭代以獲取鍵。在主方法中,我們使用 handleKeySet() 方法獲取鍵的集合。
package com.tutorialspoint; import java.util.Enumeration; import java.util.HashSet; import java.util.ListResourceBundle; import java.util.Set; // create a class that extends to ListResourceBundle class MyResources extends ListResourceBundle { // get contents must be implemented protected Object[][] getContents() { return new Object[][] { {"1", new Student(1, "Julie")}, {"2", new Student(2, "Robert")}, {"3", new Student(3, "Adam")} }; } protected Set<String> handleKeySet() { Set<String> set = new HashSet<String>(); // get the keys Enumeration<String> enumeration = getKeys(); // print the keys while (enumeration.hasMoreElements()) { set.add(enumeration.nextElement()); } return set; } } public class ListResourceBundleDemo { public static void main(String[] args) { // create a new MyResources instance MyResources mr = new MyResources(); // get the set of Keys Set<String> keys = mr.handleKeySet(); // print each key keys.forEach(System.out::println); } } 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 + " ]"; } }
輸出
讓我們編譯並執行上述程式,這將產生以下結果。
1 2 3