Interface - Map:: C. Deeksha: 23B81A7215: Aid: ADSJ (Advanced Datastructure Through Java)
Interface - Map:: C. Deeksha: 23B81A7215: Aid: ADSJ (Advanced Datastructure Through Java)
NAME : C. DEEKSHA
ROLL NO : 23B81A7215
BRANCH : AID
SUBJECT : ADSJ(Advanced Datastructure Through Java)
INTRODUCTION
• What is a 2D Collection Map?
A data structure that stores data in a two-dimensional format using key-value pairs.
• Definition:
The Map interface represents a collection of key-value pairs, where each key is unique and
maps to exactly one value. It is not a part of the Collection interface but is a separate interface
in the Java Collections Framework.
• Key Characteristics:
Key-Value Pairs: Each entry in a Map consists of a key and a value. The key is used to retrieve
the corresponding value.
Uniqueness of Keys: Each key in a Map must be unique. If you try to insert a key that already
exists, the new value will overwrite the existing value.
Null Keys and Values: Depending on the specific implementation of the Map, it may allow null
keys and values.
KEY METHODS
• put(K key, V value): Associates the specified value with the specified key in the map.
• get(Object key): Returns the value to which the specified key is mapped, or null if the map
contains no mapping for the key.
• remove(Object key): Removes the mapping for a key from the map if it is present.
• containsKey(Object key): Returns true if the map contains a mapping for the specified key.
• containsValue(Object value): Returns true if the map maps one or more keys to the specified
value.
• size(): Returns the number of key-value pairs in the map.
• isEmpty(): Returns true if the map contains no key-value pairs.
• keySet(): Returns a Set view of the keys contained in the map.
• values(): Returns a Collection view of the values contained in the map.
• entrySet(): Returns a Set view of the mappings contained in the map.
HashMap
• Uses a hash table for storage.
• Allows one null key and multiple null values.
• Does not guarantee any order of its elements.
• Average time complexity for basic operations (get, put) is O(1).
CODE
import java.util.HashMap;
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("Apple", 1);
hashMap.put("Banana", 2);
System.out.println(hashMap.get("Apple"));
// Output: 1
LinkedHashMap
• Extends HashMap and maintains a doubly-linked list of its entries.
• Maintains the order of insertion.
• Allows one null key and multiple null values.
• Slightly slower than HashMap due to the overhead of maintaining the linked list.
CODE
import java.util.LinkedHashMap;
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("Apple", 1);
linkedHashMap.put("Banana", 2);
System.out.println(linkedHashMap.get("Banana"));
// Output: 2
TreeMap
• Implements the Map interface using a red-black tree.
• Maintains a sorted order of its keys based on their natural ordering or a specified
comparator.
• Does not allow null keys but allows null values.
• Average time complexity for basic operations is O(log n).
CODE
import java.util.TreeMap;
TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 1);
treeMap.put("Banana", 2);
System.out.println(treeMap.firstKey());
// Output: Apple
DIFFERENCES