0% found this document useful (0 votes)
3 views9 pages

Interface - Map:: C. Deeksha: 23B81A7215: Aid: ADSJ (Advanced Datastructure Through Java)

The document provides an overview of the Map interface in Java, detailing its characteristics, key methods, and different implementations such as HashMap, LinkedHashMap, and TreeMap. It highlights the unique features and performance differences among these implementations, including their handling of null keys and values. Additionally, it includes code examples demonstrating the usage of each type of Map.

Uploaded by

deekshareddy716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Interface - Map:: C. Deeksha: 23B81A7215: Aid: ADSJ (Advanced Datastructure Through Java)

The document provides an overview of the Map interface in Java, detailing its characteristics, key methods, and different implementations such as HashMap, LinkedHashMap, and TreeMap. It highlights the unique features and performance differences among these implementations, including their handling of null keys and values. Additionally, it includes code examples demonstrating the usage of each type of Map.

Uploaded by

deekshareddy716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

INTERFACE - MAP

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

Feature HashMap LinkedHashMap TreeMap


Order No order Insertion order Sorted order
Null keys/values Yes Yes No null keys
Performance O(1) O(1) O(log n)
Use Case Fast access Maintain order Sorted data
CODE
import java.util.HashMap;
import java.util.Map;
public class MapDemo { OUTPUT :-
public static void main(String[] args) { Fruit Map: {Apple=10, Banana=20,
Orange=30}
Map<String, Integer> fruitMap = new HashMap<>() {{
Apple count: 10
put("Apple", 10);
Contains Banana? true
put("Banana", 20);
After removing Orange: {Apple=10,
put("Orange", 30); Banana=20}
}}; Fruit counts:
System.out.println("Fruit Map: " + fruitMap); Fruit: Apple, Count: 10
System.out.println("Apple count: " + fruitMap.get("Apple"));
Fruit: Banana, Count: 20
System.out.println("Contains Banana? " + fruitMap.containsKey("Banana"));
fruitMap.remove("Orange");
System.out.println("After removing Orange: " + fruitMap);
System.out.println("Fruit counts:");
fruitMap.forEach((key, value) -> System.out.println("Fruit: " + key + ", Count: " + value));
}
}
THANK YOU!

You might also like