Convert ImmutableMap Guava Java

Convert ImmutableMap Guava Java

Google guava is getting popular day by day as it has many in build API which can be use in active programming. In this we will see how to convert HashMap to ImmutableMap using Google Guava library. Before using this class you will to add jars in your project class path.

  • If you are using maven please include below OR whatever new version is available:
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>18.0</version>
</dependency>

For other project please include guava.version.jar and you could use maven repository here

  • ConvertImmutableMap.java:
import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.ImmutableMap;

public class ConvertImmutableMap {

	public static void main(String[] args) {
		
		Map<String, String> simpleMap = new HashMap<String, String>();
		
		simpleMap.put("Java", "Honk");
		simpleMap.put("Java1", "Honk");
		
		ImmutableMap<String, String> immutablesimpleMap = ImmutableMap.copyOf(simpleMap);
		
		immutablesimpleMap.forEach((k, v) -> System.out.println(k + "=" + v));

	}

}
  • Output:

Convert ImmutableMap Guava Java

Leave a Reply

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