In this tutorial we will learn how to use Map in Java. Map is a very useful interface in Java collection, please see the tutorial below about Map in details.

Java, one of the most widely used programming languages, offers a plethora of data structures to handle complex problems efficiently. Among these, the Map interface plays a pivotal role in simplifying data manipulation tasks.
With its ability to store key-value pairs and provide efficient retrieval and modification operations, the Map interface has become an indispensable tool for developers. In this article, we will dive into the world of Maps in Java, understanding their key features, exploring different implementations, and learning how they can enhance your programming capabilities.
Map Key-value pair
The Map interface in Java represents an object that maps keys to values, where each key-value pair is unique. It provides an abstraction to efficiently store, retrieve, and manipulate data. The Map interface does not allow duplicate keys, ensuring that each key corresponds to a unique value.
The primary methods provided by the Map interface include put(key, value)
, get(key)
, containsKey(key)
, containsValue(value)
, remove(key)
, keySet()
, and values()
. These methods enable efficient data retrieval and manipulation, making Maps suitable for a wide range of use cases.
Implementation of Map in Java
Java offers several implementations of the Map interface, each designed to meet specific requirements. Some of the most commonly used implementations include HashMap, TreeMap, and LinkedHashMap.
1. HashMap
This implementation provides a hash table-based implementation of the Map interface. It offers constant-time performance for basic operations, such as put()
and get()
. HashMap is unordered and allows null values and a single null key.
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("banana", 12);
map.put("apple", 100);
map.put("guavava", 200);
System.out.println(map);
System.out.println("is apple available = "+ map.containsKey("apple"));
System.out.println("is 250 available = "+ map.containsValue(250));
System.out.println("value of guavava = "+ map.get("guavava"));
}
}
//output-----------------------------------------
{banana=12, apple=100, guavava=200}
is apple available = true
is 250 available = false
value of guavava = 200
2. TreeMap
TreeMap is a Red-Black tree-based implementation of the Map interface. It maintains the keys in sorted order, which makes it suitable for scenarios where a sorted view of the keys is required. However, the tradeoff is slightly slower performance compared to HashMap.
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<>();
map.put("banana", 12);
map.put("apple", 100);
map.put("guavava", 200);
map.put("guavava", null);
System.out.println(map);
System.out.println("is apple available = "+ map.containsKey("apple"));
System.out.println("is 250 available = "+ map.containsValue(250));
System.out.println("value of guavava = "+ map.get("guavava"));
}
}
//output
{apple=100, banana=12, guavava=null}
is apple available = true
is 250 available = false
value of guavava = null
3. LinkedHashMap
LinkedHashMap combines the features of HashMap and LinkedList, providing a predictable iteration order based on the insertion order. It is useful when maintaining the order of insertion is crucial, along with efficient key-value pair retrieval.
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<>();
map.put("banana", 12);
map.put("apple", 100);
map.put("guavava", 200);
map.put("guavava", null);
map.put(null, null);
System.out.println(map);
System.out.println("is apple available = "+ map.containsKey("apple"));
System.out.println("is 250 available = "+ map.containsValue(250));
System.out.println("value of guavava = "+ map.get("guavava"));
}
}
//output
{banana=12, apple=100, guavava=null, null=null}
is apple available = true
is 250 available = false
value of guavava = null
Thank You for reaching out the tutorial on FlutterTpoint. If you have any questions and doubts you can comment in the comments section below. You can see more useful tutorial also from here. See Timer In Android
If you have still any doubt then you can easily understand from the video.