CS & IT
ENGINEERING
Java With OOPs
Java Collections
Lecture No-03 By- Aditya sir
Recap of Previous Lecture
Topic
Intro to collections
Artist
1
2 linked list
Topics to be Covered
Topic Java Collections Questions
Hash map
3
Topic : Basic HashMap Operations
Introduction to HashMap
• Definition: A HashMap stores key-value pairs using a hash table. It allows one
null key and multiple null values.
• Advantages
○ Fast average-time performance for get and put operations (O(1)).
○ Unique keys: Inserting a duplicate key replaces its value.
• Common Methods:
○ put(key, value), get(key), remove(key), containsKey(key), containsValue
(value), keySet(), values(), entrySet(), size(), clear()
• Use Cases:
○ Caching, configuration settings, dictionary implementations, etc.
Topic : Basic HashMap Operations
Example 1 – Creating and Populating a HashMap
• Problem Statement: Create a HashMap of country–capital pairs and print the
map.
• Hint: Use put(key, value) to add entries.
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example1 {
public static void main(String[] args) {
// Hint: Add country-capital pairs with put()
HashMap<String, String> capitals = new HashMap<>();
[Link]("USA", "Washington, D.C.");
[Link]("France", "Paris");
[Link]("Japan", "Tokyo");
[Link]("Germany", "Berlin");
[Link]("India", "New Delhi");
[Link]("Brazil", "Brasília");
[Link]("Canada", "Ottawa");
[Link]("Australia", "Canberra");
[Link]("Country Capitals: " + capitals);
}
}
Topic : Basic HashMap Operations
Expected Output (order may vary):
• Country Capitals: {Canada=Ottawa, USA=Washington, D.C., Brazil=Brasília,
India=NewDelhi, Australia=Canberra, Germany=Berlin, Japan=Tokyo, France= Paris}
Topic : Basic HashMap Operations
Example 2 – Retrieving a Value by Key
• Problem Statement: Retrieve and print the capital of "France" from the
HashMap.
• Hint: Use get(key) to access the value.
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example2 {
public static void main(String[] args) {
// Hint: Retrieve the capital using get(key)
HashMap<String, String> capitals = new HashMap<>();
[Link]("France", "Paris");
[Link]("USA", "Washington, D.C.");
String capitalOfFrance = [Link]("France");
[Link]("Capital of France: " + capitalOfFrance);
}
}
Topic : Basic HashMap Operations
Expected Output:
• Capital of France: Paris
Topic : Basic HashMap Operations
Example 3 – Updating a Value
• Problem Statement: Update the capital of "Japan" in the HashMap and print
the updated map.
• Hint: Reuse put(key, value) with the existing key to update the value.
create different
example
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example3 {
public static void main(String[] args) {
// Hint: Putting a value for an existing key updates it.
HashMap<String, String> capitals = new HashMap<>();
[Link]("Japan", "Tokyo");
[Link]("Before update: " + capitals);
[Link]("Japan", "Kyoto"); // Update capital of Japan
[Link]("After update: " + capitals);
}
}
Topic : Basic HashMap Operations
Expected Output:
• Before update: {Japan=Tokyo}
After update: {Japan=Kyoto}
Topic : Basic HashMap Operations
Example 4 – Removing an Entry
• Problem Statement: Remove the entry for "Brazil" from the HashMap and
print the resulting map
• Hint: Use remove(key) to delete the entry.
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example4 {
public static void main(String[] args) {
// Hint: Remove an entry by key.
HashMap<String, String> capitals = new HashMap<>();
[Link]("Brazil", "Brasília");
[Link]("Canada", "Ottawa");
[Link]("Australia", "Canberra");
[Link]("Before removal: " + capitals);
[Link]("Brazil");
[Link]("After removal: " + capitals);
}
}
Topic : Basic HashMap Operations
Expected Output (order may vary):
• Before removal: {Australia=Canberra, Brazil=Brasília, Canada=Ottawa}
After removal: {Australia=Canberra, Canada=Ottawa}
Topic : Basic HashMap Operations
Example 5 – Checking for a Key and Value
• Problem Statement: Check if the HashMap contains the key "India" and the
value "Paris", then print the results.
• Hint: Use containsKey(key) and containsValue(value) for verification.
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example5 {
public static void main(String[] args) {
// Hint: Verify key and value using containsKey and containsValue.
HashMap<String, String> capitals = new HashMap<>();
[Link]("India", "New Delhi");
[Link]("UK", "London");
[Link]("Contains key 'India': " + [Link]("India"));
[Link]("Contains value 'Paris': " + [Link]("Paris"));
}
}
Topic : Basic HashMap Operations
Expected Output:
• Contains key 'India': true
Contains value 'Paris': false
Topic : Basic HashMap Operations
Example 6 – Iterating Over Keys
• Problem Statement: Iterate over the keys in a HashMap and print each key
with its corresponding value.
• Hint: Use keySet() to retrieve keys, then get values with get(key).
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example6 {
public static void main(String[] args) {
// Hint: Use keySet() to iterate over keys.
HashMap<String, String> capitals = new HashMap<>();
[Link]("USA", "Washington, D.C.");
[Link]("France", "Paris");
[Link]("Japan", "Tokyo");
for (String country : [Link]()) {
[Link](country + " -> " + [Link](country));
}
}
}
Topic : Basic HashMap Operations
Expected Output (order may vary):
• USA -> Washington, D.C.
France -> Paris
Japan -> Tokyo
Topic : Basic HashMap Operations
Example 7 – Iterating Over Entries
• Problem Statement: Iterate over the key-value pairs in a HashMap using the
entry set and print each pair.
• Hint: Use entrySet() for a direct iteration over [Link] objects.
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example7 {
public static void main(String[] args) {
// Hint: Use entrySet() to iterate through key-value pairs.
HashMap<String, String> capitals = new HashMap<>();
[Link]("Germany", "Berlin");
[Link]("Italy", "Rome");
[Link]("Spain", "Madrid");
for (var entry : [Link]()) {
[Link]([Link]() + " => " + [Link]());
}
}
}
Topic : Basic HashMap Operations
Expected Output (order may vary):
• Germany => Berlin
Italy => Rome
Spain => Madrid
Topic : Basic HashMap Operations
Example 8 – Getting the Size and Clearing the Map
• Problem Statement: Print the size of a HashMap, clear it, and then print the
size again.
• Hint: Use size() to get the number of entries and clear() to empty the map.
Topic : Basic HashMap Operations
• Code:
import [Link];
public class HM_Example8 {
public static void main(String[] args) {
// Hint: size() returns the number of entries; clear() empties the map.
HashMap<String, String> capitals = new HashMap<>();
[Link]("Canada", "Ottawa");
[Link]("Australia", "Canberra");
[Link]("New Zealand", "Wellington");
[Link]("Size before clear: " + [Link]());
[Link]();
[Link]("Size after clear: " + [Link]());
}
}
Topic : Basic HashMap Operations
Expected Output:
• Size before clear: 3
Size after clear: 0
THANK - YOU