java 1.8遍历map
时间: 2023-11-30 22:42:17 浏览: 181
在Java 1.8中,可以使用forEach方法和Lambda表达式来遍历Map。以下是两种不同的演示代码:
1.使用forEach方法和匿名内部类遍历Map:
```java
Map<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
maps.put("java", 20);
maps.put("python", 17);
maps.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String key, Integer value) {
System.out.println(key + "-->" + value);
}
});
```
2.使用forEach方法和Lambda表达式遍历Map:
```java
Map<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
maps.put("java", 20);
maps.put("python", 17);
maps.forEach((key, value) -> System.out.println(key + "-->" + value));
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)