Java – Convert HashMap to/from Properties

In the post, we show how to convert Java HashMap to Properties and versus.

I. Convert Java HashMap to Properties

1. Use putAll()

-> Method signature:

public void putAll(Map t)

Sample Code:

Map map = new HashMap();

Properties properties = new Properties();
properties.put("db.username", "username");
properties.put("db.password", "password");
...

properties.putAll(map);
2. Manual Put() with for-loop
Map map = new HashMap();

Properties properties = new Properties();
properties.put("db.username", "username");
properties.put("db.password", "password");
...

for (final String key: map.keySet())
	properties.put(key, map.get(key));

II. Convert Java Properties to HashMap

1. Use HashMap’s constructor
Properties properties = new Properties();
properties.put("db.username", "username");
properties.put("db.password", "password");
...

Map map = new HashMap(properties);
2. Use Java 8 Collectors
Map map = new HashMap();

Properties properties = new Properties();
properties.put("db.username", "username");
properties.put("db.password", "password");
...

map = properties.entrySet().stream().collect(Collectors.toMap(
		            e -> String.valueOf(e.getKey()),
		            e -> String.valueOf(e.getValue())));
3. Manual Put() with for-loop
Map map = new HashMap();

Properties properties = new Properties();
properties.put("db.username", "username");
properties.put("db.password", "password");
...

for (final String name: properties.stringPropertyNames())
    map.put(name, properties.getProperty(name));

III. Full SourceCode

1. HashMap -> Properties
package com.javasampleapproach.java8.hashmap2properties;

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

public class Java8HashMap2Properties {
	public static void main(String[] args) {
		Map map = new HashMap();
		Properties properties = new Properties();
		
		map.put("db.username", "username");
		map.put("db.password", "password");
		map.put("db.driver", "org.postgresql.Driver");
		map.put("db.url", "jdbc:postgresql://localhost/testdb");
		
		// Approach 1
		// -> use putAll
		properties.putAll(map);
		
		// Traverse Properties
		properties.forEach((k, v) -> System.out.println(String.format("key=%s, value=%s", k, v)));
		/*
			key=db.driver, value=org.postgresql.Driver
			key=db.username, value=username
			key=db.url, value=jdbc:postgresql://localhost/testdb
			key=db.password, value=password
		 */
		
		// Approach 2
		// --> use for-loop
		System.out.println("-------------------------------------");
		for (final String key: map.keySet())
			properties.put(key, map.get(key));
		
		// Traverse Properties
		properties.forEach((k, v) -> System.out.println(String.format("key=%s, value=%s", k, v)));
		/*
			key=db.driver, value=org.postgresql.Driver
			key=db.username, value=username
			key=db.url, value=jdbc:postgresql://localhost/testdb
			key=db.password, value=password
		 */
	}
}

2. Properties -> HashMap
Program 1 – use HashMap’s constructor
package com.javasampleapproach.java8.hashmap2properties;

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

public class Java8Properties2HashMap {
	public static void main(String[] args) {
		Properties properties = new Properties();
		
		properties.put("db.username", "username");
		properties.put("db.password", "password");
		properties.put("db.driver", "org.postgresql.Driver");
		properties.put("db.url", "jdbc:postgresql://localhost/testdb");
		
		Map map = new HashMap(properties);
		map.forEach((k, v) -> System.out.println(String.format("key=%s, value=%s", k, v)));
		/*
			key=db.driver, value=org.postgresql.Driver
			key=db.password, value=password
			key=db.username, value=username
			key=db.url, value=jdbc:postgresql://localhost/testdb
		 */
	}
}
Program 2 – others
package com.javasampleapproach.java8.hashmap2properties;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

public class Java8Properties2HashMap {
	public static void main(String[] args) {
		Map map = new HashMap();
		Properties properties = new Properties();
		
		properties.put("db.username", "username");
		properties.put("db.password", "password");
		properties.put("db.driver", "org.postgresql.Driver");
		properties.put("db.url", "jdbc:postgresql://localhost/testdb");
		
		// Approach 1
		// --> Use Stream of Java 8
		map = properties.entrySet().stream().collect(Collectors.toMap(
				            e -> String.valueOf(e.getKey()),
				            e -> String.valueOf(e.getValue())));
							
		// Traverse Map
		map.forEach((k, v) -> System.out.println(String.format("key=%s, value=%s", k, v)));
		/*
			key=db.driver, value=org.postgresql.Driver
			key=db.password, value=password
			key=db.username, value=username
			key=db.url, value=jdbc:postgresql://localhost/testdb
		 */

		System.out.println("----------------------------------");
		// Approach 2
		map.clear();
		
		for (final String name: properties.stringPropertyNames())
		    map.put(name, properties.getProperty(name));
		
		// Traverse Map
		map.forEach((k, v) -> System.out.println(String.format("key=%s, value=%s", k, v)));
		/*
			key=db.driver, value=org.postgresql.Driver
			key=db.password, value=password
			key=db.username, value=username
			key=db.url, value=jdbc:postgresql://localhost/testdb
		 */
	}
}



By grokonez | March 4, 2018.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*