HashMap Introduction

HashMap Introduction

HashMap is a class in the java.utils package that implements the Map interface.It is used to store the key-value pair.

Some of the features of HashMap are:

  1. The keys should be unique.
  2. HashMap allows only one null key.
  3. The values can be null or duplicate.
  4. The keys are stored in random order.

codeblogs

Creating a HashMap

There are four different constructors available to create a HashMap in Java.

a> Using the no-arg constructor: The simplest way to create a HashMap is by using the no-arg constructor. This constructor creates a HashMap with an initial capacity of 16 and load factor of 0.75.

Map map = new HashMap<>();

Load factor is a number that defines when a Map should be resized. If the load factor is 0.75, then it means that the Map should be resized when it is 75 percent full.

b>Using the constructor that takes initial capacity:

We can also provide the initial capacity while creating the HashMap. If we are already aware that our HashMap will contain more than 16 elements, then it is better to provide some initial capacity as it reduces the number of resizes. In this case, also, the default load factor (0.75) is used.

c>Using the constructor that takes initial capacity and load factor:

We can also provide initial capacity with the load factor while creating the HashMap. If we don’t want frequent resizing then we can set the load factor to a higher number.

d>Using the constructor that takes another Map as a parameter:

We can also create a HashMap using another Map by passing it to the constructor. The newly created HashMap will have the same size as that of the passed Map, whereas the load factor will default to 0.75

Inserting into a HashMap

Using the put() method

We can use the **put(K key, V value)** method to insert a key-value pair in the HashMap. If the key is not present, then a new key-value pair will be added. If the key is already present, then the value will be updated.

Using the putIfAbsent() method

The **putIfAbsent(K key, V value)** method inserts a key-value pair only if it is not already present in the Map. If the key is already present then its value will not be updated. This method was added in Java 8.

Using the putAll() method

The **putAll(Map<? extends K, ? extends V> m)** method copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

<script src=”gist.github.com/codewith-fun/0049d4ca4d5528..">

Fetching an element from a HashMap

There are two ways to get an element from a HashMap.

Using the get() method

The **get(Object key**) method takes a key as a parameter and returns the value corresponding to that key. If the key is not present, it returns null.

Using the getOrDefault() method

The **getOrDefault(Object key, V defaultValue)** method is useful if are not sure whether a key is present in the Map or not. If the key is present then this method returns the value corresponding to the key and if the key is not present then the default value is returned.

Additional operations on HashMap

  • **public boolean containsKey(Object key)**: Returns **true** if this map contains a mapping for the specified key.
  • **public boolean containsValue(Object value)**: Returns true if this map maps one or more keys to the specified value.
  • **public V get(Object key)**: Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  • **public V put(K key, V value)**: Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.
  • **public void putAll(Map<? extends K, ? extends V> m)**: Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
  • **public V remove(Object key)**: Removes the mapping for a key from this map if it is present (optional operation).
  • **public boolean isEmpty()**: A utility method returning true if no key-value mappings are present in the map.
  • **public int size()**: Returns the number of key-value mappings in this map. If the map contains more than **Integer.MAX_VALUE** elements return **Integer.MAX_VALUE**.
  • **public Set<K> keySet()**: Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
  • **public Set<Map.Entry<K,V>> entrySet()**: This method returns a Set view of the HashMap mappings. This set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

https://www.linkedin.com/pulse/activity-launch-mode-laxmi-kant/?trackingId=TmgPlEBVSUi4QJsOtoMeqQ

[Machine Learning with tensorFlow lite in mobile apps
Techniques of machine learning in android appscodeblogs.medium.com](https://codeblogs.medium.com/android-machine-learning-9f1c7f4947ef "codeblogs.medium.com/android-machine-learni..")

[Activity Launch Mode
Launch mode is an instruction for Android OS which specifies how the activity should be launched. It instructs how any…codeblogs.medium.com](https://codeblogs.medium.com/activity-launch-mode-13bf4e924296 "codeblogs.medium.com/activity-launch-mode-1..")

[What can I do with kotlin : →
Well, The answers are you can any kind of development it can be like server-side, client-side web and android also this…codeblogs.medium.com](https://codeblogs.medium.com/what-can-i-do-with-kotlin-38fea7fddb5c "codeblogs.medium.com/what-can-i-do-with-kot..")

[Android Context
What is Context ? and how is it used?codeblogs.medium.com](https://codeblogs.medium.com/android-context-2371c3000c12 "codeblogs.medium.com/android-context-2371c3..")

Did you find this article valuable?

Support MCODE by becoming a sponsor. Any amount is appreciated!