Convert properties to map

Since Java Properties are typically strings but derived from a Map<Object, Object> working with them in code is irritating. This example will demonstrate how to convert Properties to Map<String, String> using core java and guava Maps utility.

Straight up Java

A less than optimal approach is passing properties to the HashMap constructor while ignoring declaring the type. If you are following good programming practice you should declare @SuppressWarnings.

Constructor

@Test
public void create_map_from_properties () {

    Properties properties = new Properties();
    properties.put("database.username", "yourname");
    properties.put("database.password", "encrypted_password");
    properties.put("database.driver", "com.mysql.jdbc.Driver");
    properties.put("database.url", "jdbc:mysql://localhost:3306/sakila?profileSQL=true");


    @SuppressWarnings({ "unchecked", "rawtypes" })
    Map<String, String> mapOfProperties = new HashMap(properties);

    assertThat(mapOfProperties.keySet(), containsInAnyOrder(
            "database.username", "database.password", 
            "database.driver", "database.url"));
}

While loop

In this snippet, we will use a while loop to iterate and add property values to a Map<String, String>.

@Test
public void create_map_from_properties_java () {
    
    Properties properties = new Properties();
    properties.put("database.username", "yourname");
    properties.put("database.password", "encrypted_password");
    properties.put("database.driver", "com.mysql.jdbc.Driver");
    properties.put("database.url", "jdbc:mysql://localhost:3306/sakila?profileSQL=true");

    Map<String, String> mapOfProperties = new HashMap<String, String>();
    
    Enumeration<?> propertyNames = properties.propertyNames();
    
    while (propertyNames.hasMoreElements()) {
      String key = (String) propertyNames.nextElement();
      mapOfProperties.put(key, properties.getProperty(key));
    }
    
    logger.info(mapOfProperties);

    assertThat(mapOfProperties.keySet(), containsInAnyOrder(
            "database.username", "database.password", 
            "database.driver", "database.url"));
}

Java 8

This snippet will show how to transform properties to a map using java 8 by first converting the entryset to a stream. Next calling the collect will reduce the stream to a map and the entryset key will be the map key and entry value the map value.

@Test
public void create_map_from_properties_java8() {

    Properties properties = new Properties();
    properties.put("database.username", "yourname");
    properties.put("database.password", "encrypted_password");
    properties.put("database.driver", "com.mysql.jdbc.Driver");
    properties.put("database.url",
            "jdbc:mysql://localhost:3306/sakila?profileSQL=true");

    Stream<Entry<Object, Object>> stream = properties.entrySet().stream();
    Map<String, String> mapOfProperties = stream.collect(Collectors.toMap(
            e -> String.valueOf(e.getKey()),
            e -> String.valueOf(e.getValue())));

    assertThat(
            mapOfProperties.keySet(),
            containsInAnyOrder("database.username", "database.password",
                    "database.driver", "database.url"));
}

Google Guava

Guava Maps, a utility class pertaining to Map instances, contains a connivence method Maps.fromProperties which allows you to get a Map<String, String> out of Properties.

@Test
public void create_map_from_properties_guava () {
    
    Properties properties = new Properties();
    properties.put("database.username", "yourname");
    properties.put("database.password", "encrypted_password");
    properties.put("database.driver", "com.mysql.jdbc.Driver");
    properties.put("database.url", "jdbc:mysql://localhost:3306/something?profileSQL=true");
    
    Map<String, String> mapOfProperties = Maps.fromProperties(properties);
    
    logger.info(mapOfProperties);
    
    assertThat(mapOfProperties.keySet(), containsInAnyOrder(
            "database.username", "database.password", 
            "database.driver", "database.url"));
}