0% found this document useful (0 votes)
18 views6 pages

Summary of Common Map Concepts

Uploaded by

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

Summary of Common Map Concepts

Uploaded by

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

Summary of Common Map Concepts

1. Understanding entrySet() and Map.Entry

 entrySet(): This method returns a set of all the key-value pairs (entries) in a
map. Each entry is represented as an instance of Map.Entry.
 Map.Entry: This is an interface that provides methods to access the key
(getKey()) and value (getValue()) of an entry.

Example: Using entrySet() and Map.Entry

Here's a simple example of how you can use entrySet() to loop over a HashMap and
access both the key and the value using Map.Entry.

java
Copy code
import java.util.*;

public class EntrySetExample {


public static void main(String[] args) {
// Create a HashMap to store names and ages
Map<String, Integer> ageMap = new HashMap<>();
ageMap.put("Alice", 25);
ageMap.put("Bob", 30);
ageMap.put("Charlie", 35);

// Use entrySet() to loop through the map


for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
String name = entry.getKey(); // Get the key (name)
Integer age = entry.getValue(); // Get the value (age)

System.out.println(name + " is " + age + " years old.");


}
}
}

Explanation:

 ageMap.entrySet() gives you all the key-value pairs (entries) in the map.
 Map.Entry<String, Integer> allows you to access each entry's key and
value.
 entry.getKey() gets the key (the name), and entry.getValue() gets the
value (the age).

2. Using getOrDefault()

 getOrDefault(): This method is used to get the value for a key in the map. If
the key does not exist, it returns a default value that you specify.

Example: Using getOrDefault()

Here's a simple example where we try to get values from a map using
getOrDefault().

java
Copy code
import java.util.*;

public class GetOrDefaultExample {


public static void main(String[] args) {
// Create a HashMap to store product prices
Map<String, Integer> productPrices = new HashMap<>();
productPrices.put("Laptop", 1000);
productPrices.put("Smartphone", 700);
productPrices.put("Tablet", 300);
// Use getOrDefault() to get a price or a default value
int laptopPrice = productPrices.getOrDefault("Laptop", 0); //
Existing key
int cameraPrice = productPrices.getOrDefault("Camera", 0); //
Non-existing key

System.out.println("Laptop price: $" + laptopPrice); //


Outputs: 1000
System.out.println("Camera price: $" + cameraPrice); //
Outputs: 0 (default value)
}
}

Explanation:

 productPrices.getOrDefault("Laptop", 0) returns the price of the laptop


because it exists in the map.
 productPrices.getOrDefault("Camera", 0) returns 0 because "Camera" is
not in the map, and the default value 0 is returned.

3. Simple Loop Using Map.Entry

Here’s a simplified version of looping through a HashMap using Map.Entry:

java
Copy code
import java.util.*;

public class SimpleLoopExample {


public static void main(String[] args) {
// Create a HashMap to store city populations
Map<String, Integer> cityPopulations = new HashMap<>();
cityPopulations.put("New York", 8000000);
cityPopulations.put("Los Angeles", 4000000);
cityPopulations.put("Chicago", 2700000);

// Loop through the map using Map.Entry


for (Map.Entry<String, Integer> entry :
cityPopulations.entrySet()) {
String city = entry.getKey(); // Get the city name
(key)
Integer population = entry.getValue(); // Get the population
(value)

System.out.println(city + " has a population of " +


population);
}
}
}

Explanation:

 cityPopulations.entrySet() returns a set of all entries (key-value pairs) in


the map.
 The for loop with Map.Entry<String, Integer> allows you to access both
the key (city) and the value (population) for each entry.

1. Iterating Over Map.Entry in a Map

This example demonstrates how to iterate over entries in a Map and access both the key and the
value.

java
Copy code
import java.util.*;

public class MapEntryExample {


public static void main(String[] args) {
// Create a map with some entries
Map<String, Integer> ageMap = new HashMap<>();
ageMap.put("Alice", 30);
ageMap.put("Bob", 25);
ageMap.put("Charlie", 35);

// Iterate over map entries


for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
String key = entry.getKey(); // Get the key
Integer value = entry.getValue(); // Get the value
System.out.println(key + ": " + value);
}
}
}

Explanation:

 Map<String, Integer> ageMap = new HashMap<>(); creates a map where the keys
are String and values are Integer.
 for (Map.Entry<String, Integer> entry : ageMap.entrySet()) iterates through
each entry in the map.
 entry.getKey() and entry.getValue() retrieve the key and value of the current entry.

2. Using Map.Entry with TreeMap

A TreeMap provides entries sorted by the natural ordering of its keys.

java
Copy code
import java.util.*;

public class TreeMapEntryExample {


public static void main(String[] args) {
// Create a TreeMap with some entries
Map<String, Integer> scoreMap = new TreeMap<>();
scoreMap.put("Alice", 85);
scoreMap.put("Bob", 90);
scoreMap.put("Charlie", 75);

// Iterate over map entries


for (Map.Entry<String, Integer> entry : scoreMap.entrySet()) {
String key = entry.getKey(); // Get the key
Integer value = entry.getValue(); // Get the value
System.out.println(key + ": " + value);
}
}
}

Explanation:

 Map<String, Integer> scoreMap = new TreeMap<>(); creates a sorted


map.
 The for loop works similarly to iterate over sorted entries.

You might also like