Let’s learn Java Map Interface

Pulsara Sandeepa
Mar 7 · 3 min read

The Map interface is not a subtype of the Collection Interface. Therefore it behaves a bit differently from the rest of the collection types.

Photo by Dariusz Sankowski on Unsplash

In my previous article, we discussed Java lists, queues, and sets in the Java Collection Interface. If you are not familiar with lists, queues, and sets. Let’s dive into Java Map Interface.

What is the Java Map Interface?

The Map interface is present in package and represents a mapping between a key and a value. The Map interface is not a subtype of the Collection Interface. Therefore it behaves a bit differently from the rest of the collection types.

A Map is an object that maps keys to values. A Map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction. The Map the interface includes methods for basic operations (such as , , , , , , and ).

Map<Key, Value> numbers = new HashMap<>();
Map<Key, Value> numbers = new LinkedHashMap<>();
Map<Key, Value> numbers = new TreeMap<>();

Hierarchy of the Map Interface

courtesy: www.javatpoint.com

Properties of the Map Interface

  • Given a key and a value we can store the value in a Map object. After the value is stored, you can retrieve it by using its key.
  • Several methods throw a when no items exist in the invoking map.
  • A is thrown when an object is incompatible with the elements in a map.
  • A is thrown if an attempt is made to use a null object and null is not allowed in the map.
  • An is thrown when an attempt is made to change an unmodifiable map.

1. HashMap

Java HashMap class implements the Map interface which allows us to store key and value pairs, where keys should be unique. If you try to insert the duplicate key, it will replace the element of the corresponding key.

HashMap<Key,Value> map1= new HashMap<>();

Essential facts about Java Hash Map

  • Contains values based on the key.
  • Contains only unique keys.
  • It may have one null key and multiple null values.
  • It maintains no order.

Let’s go through the following example to get a clear idea of Hash Maps.

2. LinkedHashMap

Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration order. It inherits the HashMap class and implements the Map interface.

LinkedHashMap<Key,Value> map1= new LinkedHashMap<>();

Essential facts about Java LinkedHash Map

  • Contains values based on the key.
  • Contains unique elements.
  • It may have one null key and multiple null values.
  • Maintains insertion order.

Let’s go through the following example to get a clear idea of LinkedHash Maps.

TreeMap

Java TreeMap class is a red-black tree-based implementation. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order.

TreeMap<Key,Value> map1= new TreeMap<>();

Essential facts about Java TreeMap

  • Contains values based on the key.
  • Contains only unique elements.
  • It cannot have a null key but can have multiple null values.
  • Maintains an ascending order.

Let’s go through the following example to get a clear idea of TreeMaps.

Conclusion

At the end of this article, we learned the basic concepts of Java Map Interface Also, we explored the advantages of the Map Interface in Java. Hope this article will help you to understand these concepts. Let’s meet with another interesting article in the near future.

Thank you!

Happy Learning 🙌😊

References

Map Interface in Java — GeeksforGeeks. https://www.geeksforgeeks.org/map-interface-java-examples/

Java — The Map Interface — Tutorialspoint. https://www.tutorialspoint.com/java/java_map_interface.htm

HashMap in Java — How it Works? https://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html

TreeMap in Java — javatpoint. https://www.javatpoint.com/java-treemap

Javarevisited

Medium’s largest Java publication, followed by 10000+ programmers. Follow to join our community.

Sign up for Javarevisited Newsletter

By Javarevisited

Collection of best Java articles, tutorials, courses, books, and resources from Javarevisite and its authors, Java Experts and many more.  Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Pulsara Sandeepa

Written by

Tech Enthusiast | Undergraduate | Blogger

Javarevisited

A humble place to learn Java and Programming better.

Pulsara Sandeepa

Written by

Tech Enthusiast | Undergraduate | Blogger

Javarevisited

A humble place to learn Java and Programming better.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store