"And at long last, I'll tell you about Map."

"Map, as you know, stores a set of key-value pairs. The keys must be unique, but the values can be anything. If you add a key-value pair to a Map, and the collection already contains the key, then the old value will be replaced with the new one. In other words, the key acts like a special index that can be any object."

Map is a mathematical term that denotes a set of (x, y) pairs, in which each unique x corresponds to some y.

Programmers are very fond of Map, so here we present 4 different collections that implement the Map interface:

"Here's its simple inheritance structure:"

Map hierarchy - 1

"The yellow indicates that Entry is a nested interface in the Map interface."

"Entry was added to describe an element pair as a single entity."

"Here are the methods of Map<K,V>:"

Methods Description
int size() Returns the number of pairs in the map.
boolean isEmpty() Checks if the map is empty.
boolean containsKey(Object key) Does the map contain the specified key?
boolean containsValue(Object value) Does the map contain the specified value?
V get(Object key) Returns the value for the specified key.
V put(K key, V value) Sets a new value for the key.
The method returns the old value or null
putAll(Map<? extends K, ? extends V> m) Adds pairs from another map.
void clear() Clears the map, i.e. removes all pairs.
Set<K>keySet() Returns a Set of keys.
Collection<V>values() Returns a collection of values.
Set<Map.Entry<K, V>>entrySet() Returns a Set of pairs.

"K and V are the type parameters for the keys and values."

"Well, I'm familiar with most of the methods, but I've only worked with some of them."

"What else can you tell me about the Entry class?"

"This class describes a pair of elements. It has a few methods:"

Methods Description
getKey() Returns the key of the key-value pair.
getValue() Returns the value of the key-value pair.
setValue(V value) Sets a new value in the key-value pair.

"Entry is convenient, because you can pass a pair to a method without passing the entire map."

"I see."

"Now I'll go over Map implementations."

"First up is HashMap. It uses hash tables to store elements. Keys and values can be of any type, as well as null. The order of elements can change when the collection changes."

"Elements are stored in a HashMap as a set of groups or buckets. An element's hashCode() method determines which bucket it falls into."

"Very roughly speaking, elements with a hash code from 1 to 100 fall into the first bucket, those with a value from 101 to 200 fall into the second, and so on."

"The point of storing elements in this way is that we can eliminate all the elements in irrelevant buckets when searching for or removing elements."

"I see."

"There's a really good article about HashMap. I recommend that you read it: https://medium.com/@mr.anmolsehgal/java-hashmap-internal-implementation-21597e1efec3"

"The second collection is LinkedHashMap. Its main difference from HashMap is that it also stores items as a linked list. In an ordinary HashMap, the order of the elements is undefined and may change over time. And you can get an iterator from a LinkedHashMap and use it to walk through all the elements in the order they were added to the LinkedHashMap. Additionally, LinkedHashMap has an iterator that lets you walk through all pairs in order of last use/activity."

"There's a really good article about LinkedHashMap. Here you go: https://medium.com/@mr.anmolsehgal/java-linkedhashmap-internal-implementation-44e2e2893036"

"The third collection I'd like to talk about today is TreeMap."

"TreeMap keeps its elements sorted in ascending order. This is achieved due to the fact that TreeMap uses a balanced red-black tree to store elements."

"As a result, insertion time and search time are very low. This class is a great choice when using very large amounts of data."

"And, of course, we have an article on TreeMap: https://medium.com/xebia-engineering/treemap-internals-199e0e0050b5"

"What haven't I told you about yet?"

"WeakHashMap, but Rishi told me about it a couple of days ago."

"When was that?"

"When he talked about SoftReference, WeakReference, and PhantomReference."

"I believe you, considering that you named them all correctly. Then go relax. See you tonight."

"Bye, Ellie."