Java for Humans {Data Structures : HashMaps}

Lincoln W Daniel
ModernNerd Code
Published in
6 min readJan 19, 2016

--

View Table of Contents | Download Supporting Code | Subscribe to ModernNerd Youtube Channel for Coding Videos | By Lincoln W Daniel

Arrays let you collect a fixed amount of elements in one data structure, and Lists can do the same but without bounds. The only thing they both don’t allow you to do is store objects and retrieve them by a specific, unique key. This is where the HashMap comes into play. If we want to make a dictionary of all the words in the English language alongside their meanings, our best bet would be to use a HashMap.

Why HashMaps are Important & Useful

Maps in Java are powerful tools, and the HashMap is perhaps one of the most powerful of them all. The HashMap class, an implementation of the Map interface, takes the best ideas of the List interface, combines it with the prowess of arrays, and gives us a tool to collect key-value pairs in one place. Elements of a HashMap are stored as pairs and it insures that no two pairs can have the same key, therefore maintaining the unique property of each of element.

The HashMap class provided to us by Java allows us to collect values of the same type and store them with unique keys. In the background, a HashMap uses arrays to store its elements. However, we don’t care about how it works in the background because all of the hard work is encapsulated. The HashMap class provides us methods to put a pair in the map, replace a pair’s value by key, remove a pair by key…

--

--

Lincoln W Daniel
ModernNerd Code

Chief Bull @ BullAcademy.org ® Elevating writers @ ManyStories.com. Author @JavaForHumans Ex: Editor in Chief MarkGrowth (acq.), Engineer @Medium @GoPuff