Java Initialize Map

This example shows how to java initialize Map with values . In this example we will initialize Map in java using constructor , map.put() method , collections and Anonymous Subclass.

How to Java Initialize Map With values ?

Now We will discuss several ways for initialize TreeMap with values as following .

    1.  Java Initialize Map – Using Constructor

In this example we will java Map initialization using using constructor . We have four type TreeMap Constructor . So we can create TreeMap using these four constructors . In this example we will create a HashMap using Constructor then add element to this HashMap Using put . After that We will  initialize TreeMap with values of HashMap . For that we will create ThreeMap using constructor with Map argument .

import java.util.HashMap;
import java.util.TreeMap;

public class TreeMapExample {
	
	public static void main(String[] args) {
		
		 HashMap<String, String> hashMap = new HashMap<String, String>();
		 hashMap.put("hcl", "amit");
		 hashMap.put("tcs","ravi");
		 hashMap.put("wipro","anmol");
		
	       TreeMap<String, String> treeMap = new TreeMap<String,String>(hashMap);
		System.out.println("treeMap is ="+ treeMap);	
		}
		
}

Output :

treeMap is ={hcl=amit, tcs=ravi, wipro=anmol}

2.  Java Initialize Map Using Put Method

In this example we initialize map in java using put() method of Map .we will create a TreeMap object and add element in this Map using put() method one by one . After that we will print the TreeMap using System.out.println() method.

import java.util.Map;
import java.util.TreeMap;

public class JavaInitializeMap {
	
	public static void main(String[] args) {
    	Map<Integer,String> treeMap = new TreeMap<Integer,String>();

    	treeMap.put(1,"java");
    	treeMap.put(2, "vogue");
    	treeMap.put(3,"welcomes");
    	treeMap.put(4,"you");
    	treeMap.put(5,"map");
       
    	//print TreeMap
		System.out.println("TreeMap :"+ treeMap);
		
		
	}

}

Output:

TreeMap :{1=java, 2=vogue, 3=welcomes, 4=you, 5=map}

3.   Java Mutable Map initialize

Now we will create mutable map in java . Firstly we will initialize TreeMap with values then create mutable map using collections .

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class JavaInitializeMap1 {
	
	public static void main(String[] args) {
		
		Map<Integer, String> mutableMap = new TreeMap<>();
		mutableMap.put(1,"java");
		mutableMap.put(2, "vogue");
		mutableMap.put(3,"welcomes");
		
	  Map<Integer, String> immutableMap = Collections.unmodifiableMap(mutableMap); 
       
		System.out.println("immutableMap : "+ immutableMap);
		
		//immutableMap.put(4, "map");
	}

}

Output:

immutableMap : {1=java, 2=vogue, 3=welcomes}

4.  Java map Initialization Using Anonymous Subclass

Now we will initialize map in java uisng anonymous subclass .But we should avoid this way to create TreeMap because first we create anonymous class here then TreeMap ,it might me create problem of memory leak. In this example we initialize map with values directly using put method as below .

import java.util.TreeMap;

public class TreeMapExample {
	
	public static void main(String[] args) {
		
		TreeMap<String, String> treeMap = new TreeMap<String, String>() {{
			put("hcl", "amit");
			put("tcs","ravi");
			put("wipro","anmol");
		}};
			
			System.out.println("treeMap is ="+ treeMap);	
		}
		
}

Output :

treeMap is ={hcl=amit, tcs=ravi, wipro=anmol}

Use the Double brace initialization to create HashMap with values we should avoid.

In this tutorial we have learned How to java initialize map using different methods .We have seen different java map initialization example with explanation here . This example is part of Java TreeMap Example  , You can see more nice ThreeMap program for practice .

Reference

Java Doc – TreeMap

Leave a Reply

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

7 + 3 =