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.

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()));
}

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.