简体   繁体   中英

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

使用新的Java 8迭代Map中所有条目的最简单方法是什么?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM