Java 8 introduced a new way to iterate over collections, specifically maps. The new method, called forEach, can be used to iterate over a map and perform operations on its entries. In this article, we will explore how to use this method to iterate over a map in Java 8.
The forEach Method
The forEach method is a new addition to the Iterable interface in Java 8. It takes a Consumer object as its parameter, which is used to perform an operation on each element in the collection. For maps, the forEach method takes a BiConsumer object, which is used to perform an operation on each key-value pair in the map.
Example:
Let's take a look at an example of using the forEach method to iterate over a map in Java 8: ``` Map map = new HashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); map.forEach((key, value) -> System.out.println(key + " " + value)); ``` This code will iterate over the entries in the map and print out each key-value pair.
The Entry Interface
The forEach method uses the Entry interface to iterate over the key-value pairs in a map. The Entry interface has two methods: getKey and getValue, which are used to retrieve the key and value of the entry, respectively.
Example:
Let's take a look at an example of using the Entry interface to iterate over a map in Java 8: ``` Map map = new HashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } ``` This code will iterate over the entries in the map and print out each key-value pair using the Entry interface.
Filtering Entries
In addition to iterating over all the entries in a map, you can also filter the entries using the Stream API introduced in Java 8.
Example:
Let's take a look at an example of using the Stream API to filter entries in a map in Java 8: ``` Map map = new HashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); map.entrySet().stream() .filter(entry -> entry.getValue() % 2 == 0) .forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue())); ``` This code will iterate over the entries in the map, filter out the entries where the value is odd, and print out the remaining key-value pairs.
Question & Answer
Q: What is the forEach method in Java 8?
A: The forEach method is a new addition to the Iterable interface in Java 8. It takes a Consumer object as its parameter, which is used to perform an operation on each element in the collection.
Q: How do you iterate over a map in Java 8?
A: You can use the forEach method to iterate over a map in Java 8. The forEach method takes a BiConsumer object as its parameter, which is used to perform an operation on each key-value pair in the map.
Q: How do you filter entries in a map in Java 8?
A: You can use the Stream API to filter entries in a map in Java 8. The filter method can be used to specify a predicate to filter the entries, and the forEach method can be used to perform an operation on the remaining entries.