Java ListResourceBundle Class



Introduction

The Java ListResourceBundle class is an abstract subclass of ResourceBundle that manages resources for a locale in a convenient and easy to use list.

Class declaration

Following is the declaration for java.util.ListResourceBundle class −

public abstract class ListResourceBundle
   extends ResourceBundle

Field

Fields inherited from class java.util.ResourceBundle.

Class constructors

Sr.No. Constructor & Description
1

ListResourceBundle()

This is the only constructor.

Class methods

Sr.No. Method & Description
1 protected abstract Object[][] getContents()

This method returns an array in which each item is a pair of objects in an Object array.

2 Enumeration<String> getKeys()

This method returns an Enumeration of the keys contained in this ResourceBundle and its parent bundles.

3 Object handleGetObject(String key)

This method gets an object for the given key from this resource bundle.

4 protected Set<String> handleKeySet()

This method returns a Set of the keys contained only in this ResourceBundle.

Methods inherited

This class inherits methods from the following classes −

  • java.util.ResourceBundle
  • java.util.Object

Getting and iterating Keys of ListResourceBundle Example

The following example shows the usage of Java ListResourceBundle getKeys() method to iterate keys of the ListResourceBundle. We've created a ListResourceBundle object having contents an two dimensional array of String, Integer. Then few entries are added by overriding getContents() method and then an enumeration of keys is retrieved and iterated to print the keys.

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.ListResourceBundle;

// 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}
      };
   }
}

public class ListResourceBundleDemo {
   public static void main(String[] args) {

      // create a new MyResources instance
      MyResources mr = new MyResources();

      // get the keys
      Enumeration<String> enumeration = mr.getKeys();

      // print the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement()); 
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result.

1
2
3
Advertisements