---
title: "Map and HashMap in Java"  
description: "Hash table based implementation of the Map interface.This implementation provides all of the optional map operations, and permits null values and the"  
author: "Manoj Pandey"  
published: 2015-04-14  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/1720/map-and-hashmap-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Map and HashMap in Java

[Hash table](https://www.mindstick.com/forum/157906/what-is-the-difference-between-a-hash-table-and-an-array-when-would-you-use-one-over-the-other) based [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) of the Map [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp). This implementation provides all of the [optional](https://www.mindstick.com/interview/22851/main-difference-between-optional-and-required-keywords) map [operations](https://www.mindstick.com/news/2675/bmw-agrees-to-integrate-blockchain-with-operations-partners-with-bnb-chain-coinweb), and permits [null values](https://www.mindstick.com/forum/159818/how-to-use-the-coalesce-function-to-handle-null-values-in-sql-queries) and the null key. It contains only unique elements. It may have one null key and [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) null values. It maintains no order.\

Map is interface and hashmap is class. All the [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) of Map, discussed earlier, are attained by HashMap.

HashMap<String, Object> map1 = new HashMap<String, Object>();

Map<String, Object> map2 = new HashMap<String, Object>();

First of all Map is an interface it has different implementation like - HashMap, TreeHashMap, LinkedHashMap etc. Interface works like a super class for the implementing class. So according to OOP's rule any [concrete class](https://www.mindstick.com/forum/158821/what-is-a-concrete-class-and-how-is-it-different-from-an-abstract-class) that implements Map is a Map also. That means we can assign/put any HashMap type [variable](https://www.mindstick.com/blog/11493/what-is-a-variable) to a Map type variable without any type of casting.

##### In this case we can assign map1 to map2 without any casting or any losing of data –

map2=map1;

![Map and HashMap in Java](https://www.mindstick.com/mindstickarticle/6be16765-bb37-41ea-bdfd-d1ad52bf22cf/images/8d40b4d4-5e08-47a7-9d54-b524fc6a2b09.png)

##### Map having following implementations,

HashMap - Map m = new HashMap();

LinkedHashMap - Map m = new LinkedHashMap();

Tree Map - Map m = new TreeMap();

WeakHashMap - Map m = new WeakHashMap();

```
import java.util.HashMap; public class Sample {     static HashMap<Integer, String> myMap;      public static void main(String args[]) {          myMap = new HashMap<Integer, String>();          // add data to haspmap          myMap.put(101, "Zack");          myMap.put(102, "Jooli");          myMap.put(103, "David");          myMap.put(104, "Rusell");          myMap.put(105, "Robin");           // find data from haspmap by key          System.out.print(myMap.get(105));         //if record not found then return null.     } } 
```

##### What is difference between HashSet and HashMap?

##### \
· HashSet contains only values whereas HashMap contains entry (key and value).

##### \

· Hashtable is synchronized, whereas HashMap is not. This makes HashMap better

for non-threaded applications, as unsynchronized Objects typically perform better

than synchronized ones.

##### \
· Hashtable does not allow null keys or values. HashMap allows one null key and

##### any number of null values.

##### \
· One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want

##### predictable iteration order (which is insertion order by default), you could easily

##### swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were

##### using Hashtable.

---

Original Source: https://www.mindstick.com/articles/1720/map-and-hashmap-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
