The HashMap.values()
method in Java is used to return a Collection view of the values contained in the HashMap
.
Table of Contents
- Introduction
values
Method Syntax- Examples
- Iterating Over Values in a HashMap
- Real-World Use Case: Calculating the Total Population
- Conclusion
Introduction
The HashMap.values()
method is a member of the HashMap
class in Java. It provides a Collection view of the values contained in the HashMap
. This can be useful for iterating over the values or performing bulk operations on the values without concern for the keys.
values() Method Syntax
The syntax for the values
method is as follows:
public Collection<V> values()
- The method does not take any parameters.
- The method returns a Collection view of the values contained in the
HashMap
.
Examples
Iterating Over Values in a HashMap
The values
method can be used to retrieve a Collection of the values in a HashMap
, which can then be iterated over.
Example
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class ValuesExample {
public static void main(String[] args) {
// Creating a Map with String keys and Integer values
Map<String, Integer> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", 91);
countries.put("United States", 1);
countries.put("Canada", 1);
// Getting the collection of values
Collection<Integer> values = countries.values();
// Iterating over the values
for (Integer value : values) {
System.out.println("Value: " + value);
}
}
}
Output:
Value: 91
Value: 1
Value: 1
Real-World Use Case: Calculating the Total Population
In a real-world scenario, you might use the values
method to retrieve all values from a HashMap
and perform calculations such as finding the total population of countries stored in the map.
Example
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class TotalPopulation {
public static void main(String[] args) {
// Creating a Map with String keys (country names) and Integer values (population in millions)
Map<String, Integer> countryPopulations = new HashMap<>();
// Adding entries to the HashMap
countryPopulations.put("India", 1391); // Population in millions
countryPopulations.put("United States", 331); // Population in millions
countryPopulations.put("Canada", 38); // Population in millions
// Getting the collection of values (populations)
Collection<Integer> populations = countryPopulations.values();
// Calculating the total population
int totalPopulation = populations.stream().mapToInt(Integer::intValue).sum();
// Printing the total population
System.out.println("Total Population (in millions): " + totalPopulation);
}
}
Output:
Total Population (in millions): 1760
Conclusion
The HashMap.values()
method in Java provides a way to retrieve a Collection view of the values contained in the HashMap
. By understanding how to use this method, you can efficiently access and process the values in your map. This method is useful in various scenarios, such as iterating over values, performing calculations, and analyzing data stored in a HashMap
. Using lambda expressions and streams with this method makes the code more concise and readable.