The Coordinator
The Coordinator

Reputation: 13137

Using Java 8, what is the most concise way of iterating through all the entries in a map?

What is the easiest way of iterating through all the entries in a Map using the new Java 8?

Upvotes: 1

Views: 225

Answers (1)

The Coordinator
The Coordinator

Reputation: 13137

I believe this is the most concise and easiest way to iterate through all the entries in a Map:

Create map:

 Map map = new HashMap() {{
            put("key1", "value1");
            put("key2", "value2");
        }};

Go through all the keys and values:

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

Upvotes: 5

Related Questions