Initialize HashMap In Java

In previous post we have learn java about java hashmap with example . In this article we will see how to java initialize hashmap with values . We will discuss various method for java hashmap initialization with examples

Java Initialize HashMap

We can initialize hashmap java using below methods . Let’s see java program for initialize hashmap in java using different methods .

Java Initialize HashMap Using Constructor

We can initialize hashmap java using the constructor in four different ways.                                1.  HashMap() – This is default constructor , its create an empty HashMap with the default initial capacity (16) and the default load factor (0.75). In this example we create hashmap java fist then add values in hashmap using put() method . In this java program we are initializing hashmap java using constructor.

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

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

Output:

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

2. HashMap(int initialCapacity) -It is create an empty HashMap with the specified initial capacity and the default load factor (0.75). Now we create map in java with 3 capacity and then add element into element into HashMap. Let’s see java program for initializing hashmap .

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

public class HashMapExample {
	public static void main(String[] args) {
	    
	 Map<String, String> hashMap = new HashMap<String, String>(3);
		hashMap.put("virat", "cricket");
		hashMap.put("amit","football");
		hashMap.put("ravi","tennis");
		System.out.println("hashmap is ="+ hashMap);	
	}
  }

output:

hashmap is ={virat=cricket, amit=football, ravi=tennis}

3. HashMap(int initial capacity, float loadFactor) – It is create an empty HashMap with the specified initial capacity and load factor. In this example initializing hashmap java and we stores the elements as key/value pairs using put method .

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

public class HashMapExample {
	public static void main(String[] args) {
	    
	    Map<String, String> hashMap = new HashMap<String,String>(3, 0.5f);
		hashMap.put("hcl", "amit");
		hashMap.put("tcs","ravi");
		hashMap.put("wipro","anmol");
		
		System.out.println("hashmap is ="+ hashMap);	
	}
  }

Output:

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

4. HashMap(Map map) – It is initialize hashmap in java using another map . In this example initialize hashmap java with the element of map given as argument in constructor . Here java initialize HashMap with values of map’s element .

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

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

Output:

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

Singleton HashMaps using Collections

Collections provides collections.singletonmap() method . It is returns an immutable map, contains only the specified key to the specified value. The returned map is serializable. Return map will throw an UnsupportedOperationException if any modify operation is performed on it. In this example java initialize hashmap with values given .

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

public class HashMapExample {
	public static void main(String[] args) {
		
     Map<String, String> immutableMap = Collections.singletonMap("rohit", "cricket");
		
     System.out.println("hashset is ="+ immutableMap);
	}
  }

Output:

hashset is ={rohit=cricket}

Empty HashMaps using Collections

We can create empty map using Collections.emptyMap() method . This method returns an empty map (immutable). This map is serializable. Let’s see HashmMap declaration in java using emptyMap() method here.

Map<String, String> emptyMap = Collections.emptyMap();

map will throw an UnsupportedOperationException if any modify operation is performed on it.

Creating Immutable HashMap

Java Collections provides unmodifiableMap()method this method returns an unmodifiable view of the specified map. In this method first we create a normal map then create unmodifiable map from original one.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

Output :

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

Because here  we creating two map , so it take more memory.We should avoid this practice for creating map.

Java HashMap Initialization Using Anonymous Subclass

We can initialize hashmap java with the help of anonymous subclass . But we should avoid this way to initializing hashmap java because first we create anonymous class here then map , it might me create problem of memory leak. We stores the elements as key/value pairs in the map using put method.

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

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

Output:

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

In this tutorial we learned various method for initialize hashmap in java . We have seen we can java initialize hashmap with values and stores the elements as key/value pairs using put method . Now you should be clear about java hashmap initialization.

A Guide To HashMap                                                                                                               A Guide To Java Collection                                                                                                      Java Docs – HashMap

Leave a Reply

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

+ 80 = 83