Java Maps

Avindu Dharmawardhana
5 min readDec 9, 2023

--

Map Interface

Map consists with two parts, key and value. Each key consisted with some value.

In java, HashMap and SortedMap both using to implement map interface.

· HashMap

· LinkedHashMap

· TreeMap ,are the main three classes that implemented by using Map interface.

· Map doesn’t allow duplicate keys, but map allows to store duplicate values.

· HashMap and LinkedHashMap allow null keys and values

· TreeMap doesn’t allow any null key or value.

HashMap

In the above diagram, shows a simple hashmap.

Simply, HashMap is a collection of (key, value) pairs.

HashMap is denoted by HashMap<K,V>

K-key

V-value

HashMap doesn’t allow duplicate keys but can contains duplicate values.

As shown in the above diagram, HashMap class extends AbstractMap class and it implements the Map interface.

Let’s focus on some key points of HashMap class.

· HashMap contains values based on keys.

· HashMap contains only unique keys.

· HashMap only have one null key, but have multiple null values.

· HashMap is a non-synchronized class.

· HashMap doesn’t maintains a order.

· Initial capacity of Java HashMap is 16.

In the above code shows different hashmaps with different types of key and value pairs.

Let’s focus on some important methods related to HashMap class.

1.put()

2.get()

Above code shows how to retrieve the object values in the hashmap based on the keys in the hashmap.

Based on above code, hashmap allows duplicate values but it doesn’t allow duplicate keys.

3.containsKey() and containsValue()

In the above diagram, containsKey() and containsValue() methods allow to check wheteher the given Key exist and given Value(object) exist in the hashmap.

4.remove()

Based on the above diagram, remove() method can used to remove the value for the specified key.

After removing the value from the hashmap, it maintains the remaining <key,value> pairs.

5.putAll()

Above code shows how to put all the key,value pairs in to another collection using addAll() method.

Size of the both collections are equal, in Vegetables hashmap used the same keys to stoe the values.

According to “no duplicate keys in hashnmap” when we adding the elements in fruits collection into Vegetables collection. Key value assigned values are not changing.

As shown in the above diagram, we can fix the key problem using different set of keys in the Vegetables collection.

6.replace()

In the above code shows, how to replace a new value in a existing key.

7.keySet() and values()

keyset() method will return a list of keys in the hashmap collection and values() will return a list of values() in the hashmap collection as well.

8.clear()

9.isEmpty()

10.entrySet()

As shown in the above code, entrySet() used to get a separate view of the mappings in the hashmap.

LinkedHashMap

As above image shows LinkedHashMap class is a subclass of HashMap class.

Let’s see about some important points about LinkedHashMap class.

  • Java LinkedHashMap contains values based on the key.
  • Java LinkedHashMap contains unique elements.
  • Java LinkedHashMap may have one null key and multiple null values.
  • Java LinkedHashMap is non synchronized.
  • Java LinkedHashMap maintains insertion order.
  • The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.

Above code shows how to instantiate LinkedHashMap class objects with different Generic types.

But, LinkedHashMap class implements with Hashtable, which means its indexing process happens with a Hash function.

LinkedHashMap maintains the insertion order and it keeps the <Key,Value> pair in a doubly linked list.

LinkedHashMap initially has a 16 index capacity, each node storing the key,value pair, each node pointing to the previous and next nodes.

1.Put():used to put key,value pairs in to the LinkedHashMap

2.get():return the object value of the given key

3.keySet():return only the keys in the LinkedHashmap

4.values(): return the values in the LinkedHashmap

6.remove():remove key,value pair from the LinkedHashMap when the key given

7.clear(): clear the whole LInkedHashMap and returns a empty LinkedHashMap.

--

--