Skip to content

Iterate Map in Java 8 using Stream API

In this code snippet, you will learn how to iterate a map in Java 8 using Stream API. You can convert your entry set to a stream and then iterate over it using forEach.

Advertisements
public void getValuesFromMap(Map<String, Integer> map) {
    map.entrySet().stream()
      // ... your filter or other chain methods here
      .forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
}
See also  Exception in thread "main" org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.