How to create HashMap In Java

In previous post we have learn java about hashmap methods in java and java hashmap example . Now In this article we will see how to initialize hashmap with size , how to initialize hashmap with values and how to inline hashmap initialize in java .

Tutorial Contents

HashMap using Constructor

We can initialize HashMap in 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).

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).

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.

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 create a new HashMap and initializes y element of the given map .

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 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.

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.

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.

Anonymous Subclass to Create HashMap

We can create map with the help of anonymous subclass . But we should avoid this way to create hashmap because first we create anonymous class here then map , it might me create problem of memory leak.

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}

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 *

87 − 86 =