Java ResourceBundle.handleKeySet()

Syntax

ResourceBundle.handleKeySet() has the following syntax.

protected Set < String > handleKeySet()

Example

In the following code shows how to use ResourceBundle.handleKeySet() method.


import java.util.Enumeration;
import java.util.HashSet;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.StringTokenizer;
/*from  w ww  .  j a va  2  s. c om*/
 class MainResourceBundle extends ResourceBundle {

  @Override
  public Object handleGetObject(String key) {
    if (key.equals("hello")) {
      return "Hello from java2s.com!";
    } else {
      return null;
    }
  }
  @Override
  public Enumeration getKeys() {
    StringTokenizer key = new StringTokenizer("Hello from java2s.com!");
    return key;
  }

  protected Set<String> handleKeySet() {
    return new HashSet<String>();
  }
}
 public class Main{
    public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = MainResourceBundle.getBundle("hello", Locale.US);

      // print the keys
      Enumeration<String> enumeration = bundle.getKeys();
      while (enumeration.hasMoreElements()) {
        System.out.println(enumeration.nextElement());
      }

    }
 }