Java Merge Properties into Map Example

In this short article, we will show you how to merge the given Properties instance into the given Map.
Let's write a generic method so that it can adapt any data type.

Java Merge Properties into Map Example

Let's create mergePropertiesIntoMap(Properties props, Map<K, V> map) method which takes Properties and Map as arguments.
     /**
     * Merge the given Properties instance into the given Map,
     * copying all properties (key-value pairs) over.
     * <p>Uses {@code Properties.propertyNames()} to even catch
     * default properties linked into the original Properties instance.
     * @param props the Properties instance to merge (may be {@code null})
     * @param map the target Map to merge the properties into
     */
    @SuppressWarnings("unchecked")
    public static < K, V > void mergePropertiesIntoMap(Properties props, Map < K, V > map) {
        if (props != null) {
            for (Enumeration << ? > en = props.propertyNames(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                Object value = props.get(key);
                if (value == null) {
                    value = props.getProperty(key);
                }
                map.put((K) key, (V) value);
            }
        }
    }

Complete Program

Here is a complete program to demonstrate usage of above method:
package com.javaguides.corejava.string;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * Java program to merge the given Properties instance into the given Map.
 * @author Ramesh Fadatare
 *
 */
public class MergePropertiesIntoMap {

    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.put("key1", "value1");
        properties.put("key2", "value2");
        properties.put("key3", "value3");
        properties.put("key4", "value4");
        properties.put("key5", "value5");
        properties.put("key6", "value6");
        properties.put("key7", "value7");

        Map < String, String > map = new HashMap < > ();
        mergePropertiesIntoMap(properties, map);

        for (Entry < String, String > entry: map.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
    /**
     * Merge the given Properties instance into the given Map,
     * copying all properties (key-value pairs) over.
     * <p>Uses {@code Properties.propertyNames()} to even catch
     * default properties linked into the original Properties instance.
     * @param props the Properties instance to merge (may be {@code null})
     * @param map the target Map to merge the properties into
     */
    @SuppressWarnings("unchecked")
    public static < K, V > void mergePropertiesIntoMap(Properties props, Map < K, V > map) {
        if (props != null) {
            for (Enumeration < ? > en = props.propertyNames(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                Object value = props.get(key);
                if (value == null) {
                    value = props.getProperty(key);
                }
                map.put((K) key, (V) value);
            }
        }
    }
}
Output:
key1 value1
key2 value2
key5 value5
key6 value6
key3 value3
key4 value4
key7 value7

Similar Collections Examples [Snippet]

Comments