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
java_util_listresourcebundle.htm
廣告