简体   繁体   English

使用Java 8,迭代地图中所有条目的最简洁方法是什么?

[英]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: 我认为这是迭代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) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java 8,打印文件中所有行的最优选和简洁方法是什么? - Using Java 8, what is the most preferred and concise way of printing all the lines in a file? 使用Java 8,创建排序和分组字符串列表的最简洁方法是什么 - Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings Java中最简洁的方法是从“AlphaSuffix”中获取“Alpha”? - What's the most concise way in Java to get the “Alpha” out of “AlphaSuffix”? 编写此 java 代码的最简洁/最佳方式是什么? - What's the most concise / best way to write this java code? Java不会完全遍历数组 - Java not iterating all the way through array 在 Java 中比较版本的最简洁方法 - Most concise way of comparing versions in Java 在没有java.lang.Map API的情况下迭代Hashtable条目的最佳方法是什么? - What is the BEST way to iterate through Hashtable entries WITHOUT the java.lang.Map APIs? 用反序列化编写Java类以表示简单的JSON文档的最简洁方法是什么? - What is the most concise way to write a Java class to represent a simple JSON document, with deserialization? 在 Java 8 中是否有更简洁的方法来执行此操作 - 地图操作 - Is there a more concise way to do this in Java 8 - Map manipulation 从地图中获得最高n个数字的最简洁方法? - Most concise way to get highest n numbers out of a map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM