Map .

What Does Map Do In Java?

Written by Ben Javu Jun 30, 2022 · 3 min read
What Does Map Do In Java?

Java is an object-oriented programming language that is widely used by developers. Maps are one of the most frequently used data structures in Java. A Map is an interface that maps keys to values. It is a collection of key-value pairs where each key is unique and corresponds to a single value.

Table of Contents

Carte de Java Java map Indonésie, Java, Carte
Carte de Java Java map Indonésie, Java, Carte from www.pinterest.jp

Java is an object-oriented programming language that is widely used by developers. Maps are one of the most frequently used data structures in Java. A Map is an interface that maps keys to values. It is a collection of key-value pairs where each key is unique and corresponds to a single value.

Types of Maps in Java

There are three types of maps in Java:

1. HashMap

The HashMap class implements the Map interface and provides a hash table implementation of the Map interface. It stores key-value pairs in an unordered manner and allows null for both keys and values. It provides constant-time performance for the basic operations such as get and put.

2. TreeMap

The TreeMap class implements the Map interface and provides a tree-based implementation of the Map interface. It stores key-value pairs in a sorted manner based on the natural ordering of the keys or a Comparator provided by the user. It does not allow null keys and provides log(n) time complexity for basic operations such as get and put.

3. LinkedHashMap

The LinkedHashMap class implements the Map interface and provides a hash table implementation of the Map interface with a predictable iteration order. It stores key-value pairs in the order in which they are inserted and allows null for both keys and values. It provides constant-time performance for the basic operations such as get and put.

Common Methods of Map Interface

The Map interface provides several methods to manipulate the key-value pairs. Some of the common methods are:

1. put(K key, V value)

Adds the specified key-value pair to the map. If the key already exists, the value is replaced with the new value.

2. get(Object key)

Returns the value associated with the specified key. If the key is not present in the map, it returns null.

3. remove(Object key)

Removes the key-value pair associated with the specified key from the map. If the key is not present in the map, it returns null.

4. size()

Returns the number of key-value pairs present in the map.

Example Usage

Let's see a simple example of using a HashMap in Java:

Map map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("orange", 3); System.out.println(map.get("apple")); // 1 System.out.println(map.size()); // 3

Question & Answer

Q. What is the difference between HashMap and TreeMap?

A. The main difference between HashMap and TreeMap is the order in which the key-value pairs are stored. HashMap stores them in an unordered manner, while TreeMap stores them in a sorted manner based on the natural ordering of the keys or a Comparator provided by the user.

Q. Can we store null values in a Map?

A. Yes, we can store null values in a HashMap and LinkedHashMap, but not in a TreeMap, as it does not allow null keys.

Q. What is the time complexity of HashMap and TreeMap?

A. The time complexity of basic operations such as get and put in HashMap is O(1) on average, while in TreeMap, it is O(log n).

Read next