How do I convert Properties into Map?

package org.kodejava.example.util;

import java.util.Properties;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

public class PropertiesToMap {
    public static void main(String[] args) {
        // Create a new instance of Properties.
        Properties properties = new Properties();

        // Populate properties with a dummy application information
        properties.setProperty("app.name", "HTML Designer");
        properties.setProperty("app.version", "1.0");
        properties.setProperty("app.vendor", "HTML Designer Inc");

        // Create a new HashMap and pass an instance of Properties. Properties
        // is an implementation of a Map which keys and values stored as in a
        // String.
        Map<Object, Object> map = new HashMap<>(properties);

        // Get the entry set of the Map and print it out.
        Set propertySet = map.entrySet();
        for (Object o : propertySet) {
            Map.Entry entry = (Map.Entry) o;
            System.out.printf("%s = %s%n", entry.getKey(), entry.getValue());
        }
    }
}

How do I convert ResourceBundle to Map?

The following code snippet will convert a resource bundle into a map object, a key-value mapping. It will read from a file called Messages_en_GB.properties which corresponding to the Locale.UK. For example the file contain the following string. This file should be placed under the resources directory.

welcome.message = Hello World!
package org.kodejava.example.util;

import java.util.*;

public class ResourceBundleToMap {
    public static void main(String[] args) {
        // Load resource bundle Messages_en_GB.properties from the classpath.
        ResourceBundle resource = ResourceBundle.getBundle("Messages", Locale.UK);

        // Call the convertResourceBundleTopMap method to convert the resource
        // bundle into a Map object.
        Map<String, String> map = convertResourceBundleToMap(resource);

        // Print the entire contents of the Map.
        for (String key : map.keySet()) {
            String value = map.get(key);
            System.out.println(key + " = " + value);
        }
    }

    /**
     * Convert ResourceBundle into a Map object.
     *
     * @param resource a resource bundle to convert.
     * @return Map a map version of the resource bundle.
     */
    private static Map<String, String> convertResourceBundleToMap(ResourceBundle resource) {
        Map<String, String> map = new HashMap<>();
        Enumeration<String> keys = resource.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            map.put(key, resource.getString(key));
        }
        return map;
    }
}

How do I use the HashMap class?

This examples demonstrate you how to use the HashMap class to store values in a key-value structure. In this example we store a map of error codes with their corresponding description. To store a value into the map we use the put(key, value) method and to get it back we use the get(key) method. And we can also iterate the map using the available key sets of the map.

package org.kodejava.example.util;

import java.util.HashMap;
import java.util.Map;

public class HashMapDemo {
    public static void main(String[] args) {
        Map<String, String> errors = new HashMap<>();

        // mapping some data in the map
        errors.put("404", "Resource not found");
        errors.put("403", "Access forbidden");
        errors.put("500", "General server error");

        // reading data from the map
        String errorDescription = errors.get("404");
        System.out.println("Error 404: " + errorDescription);

        // Iterating the map by the keys
        for (String key : errors.keySet()) {
            System.out.println("Error " + key + ": " + errors.get(key));
        }
    }
}

The result of the code snippet above:

Error 404: Resource not found
Error 500: General server error
Error 403: Access forbidden
Error 404: Resource not found