Using lambda expressions with HashMap in Java 8

Java 8 introduced lambda expressions, which allows us to write more concise and expressive code. One area where lambda expressions can be particularly useful is when working with collections, such as HashMaps. In this blog post, we will explore how to use lambda expressions with HashMaps in Java 8.

Table of Contents

  1. What is a HashMap?
  2. Using lambda expressions with HashMap
  3. Conclusion

What is a HashMap?

A HashMap is a class in Java that implements the Map interface and stores key-value pairs. It provides constant-time performance for basic operations such as putting and retrieving elements, making it efficient for large amounts of data.

Using lambda expressions with HashMap

1. Iterating over HashMap

Using lambda expressions, we can easily iterate over the entries of a HashMap. Here is an example:

Map<String, Integer> map = new HashMap<>();
map.put("John", 25);
map.put("Alice", 30);
map.put("Bob", 35);

map.forEach((key, value) -> System.out.println(key + ": " + value));

In the above code, the forEach method of HashMap takes a lambda expression as an argument, which iterates over each entry and prints the key-value pair.

2. Filtering HashMap entries

Lambda expressions can also be used to filter HashMap entries based on certain conditions. Here is an example:

Map<String, Integer> map = new HashMap<>();
map.put("John", 25);
map.put("Alice", 30);
map.put("Bob", 35);

map.entrySet().stream()
    .filter(entry -> entry.getValue() > 28)
    .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));

In the above code, we use the stream() method to convert the entrySet of the HashMap into a stream. Then, we apply a filter using a lambda expression to only keep entries where the value is greater than 28. Finally, we use forEach to print the filtered entries.

3. Updating HashMap values

Lambda expressions can be used to update the values of HashMap based on certain conditions. Here is an example:

Map<String, Integer> map = new HashMap<>();
map.put("John", 25);
map.put("Alice", 30);
map.put("Bob", 35);

map.replaceAll((key, value) -> value + 1);

map.forEach((key, value) -> System.out.println(key + ": " + value));

In the above code, the replaceAll method takes a lambda expression that updates each value by incrementing it by 1. We then use forEach to print the updated key-value pairs.

Conclusion

Lambda expressions in Java 8 provide a powerful and concise way to work with HashMaps. Using lambda expressions, we can easily iterate over entries, filter them based on conditions, and update their values. This makes our code more expressive and readable. Happy coding!

References:

#java #lambda-expressions