jdk1.8中的for循环问题记录
作者:山河永慕~ 时间:2023-10-16 04:08:34
jdk1.8中的for循环
jdk1.8 从语法角度,感觉发生的变化还是蛮大的。在此记录一下。
for 循环
public static void main(String[] args) {
List<Animal> list = new ArrayList<Animal>();
list.add(new Animal("miamiao",2));
list.add(new Animal("wangwang",4));
//1.8 a为泛型中的对象
list.forEach(a ->{
System.out.println(a.getName());
});
//1.5 增强行for循环
for (Animal a : list) {
System.out.println(a.getName());
}
//普通循环
for(int i =0;i<list.size();i++){
System.out.println(list.get(i).getName());
}
}
jdk1.8 新特性之 forEach 循环遍历
1、Foreach操作List
List<Integer> numbers = new ArrayList<>();
//no.1
for(Integer number : numbers){
System.out.println(number);
}
//no.2
for(int index=0,len=numbers.size();index<len;index++){
System.out.println(numbers.get(index));
}
使用jdk1.8后,可这么写
//no.1
numbers.forEach((Integer integer) -> {
System.out.println(integer);
});
//no.2
numbers.forEach(integer -> {
System.out.println(integer);
});
//no.3
numbers.forEach(integer -> System.out.println(integer));
//no.4
numbers.forEach(System.out::println);
//no.5
numbers.forEach(new MyConsumer());
2、 Foreach操作Map
来源:https://www.cnblogs.com/shanheyongmu/p/8351258.html
标签:jdk1.8,for,forEach,循环
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入了解Java核心类库--BigDecimal和System类
2023-12-18 01:50:28
详解基于spring多数据源动态调用及其事务处理
2023-06-23 14:37:25
![](https://img.aspxhome.com/file/2023/0/57530_0s.png)
C#实现简易计算器功能(附源码)
2021-07-18 00:16:00
![](https://img.aspxhome.com/file/2023/9/108659_0s.jpg)
利用C#实现SSLSocket加密通讯的方法详解
2023-03-01 02:23:05
![](https://img.aspxhome.com/file/2023/4/102274_0s.png)
Android 4.0 设置全屏修改的解决方法
2022-11-24 17:08:12
在C#中捕获内存不足异常
2021-07-14 11:50:46
C#用Topshelf创建Windows服务的步骤分享
2022-10-19 00:47:58
![](https://img.aspxhome.com/file/2023/4/76414_0s.png)
Android 判断是否连接成功了指定wifi
2021-10-21 04:59:10
java线程池对象ThreadPoolExecutor的深入讲解
2023-05-15 06:49:51
![](https://img.aspxhome.com/file/2023/7/64027_0s.png)
VsCode配置java环境的详细图文教程
2022-03-29 00:56:32
![](https://img.aspxhome.com/file/2023/8/132188_0s.jpg)
Android ViewPager导航小圆点实现无限循环效果
2022-07-09 13:10:33
记一次线程爆满导致服务器崩溃的问题排查及解决
2022-09-23 06:43:09
![](https://img.aspxhome.com/file/2023/4/125554_0s.png)
Spring中基于xml的AOP的详细步骤
2022-10-17 13:38:32
Android实现系统语言切换功能
2022-10-24 07:06:34
Android实现掷骰子效果
2022-06-16 01:09:16
![](https://img.aspxhome.com/file/2023/6/103926_0s.gif)
详解Eclipse提交项目到GitHub以及解决代码冲突
2022-05-15 09:04:52
![](https://img.aspxhome.com/file/2023/6/78966_0s.png)
c# 通过内存映射实现文件共享内存的示例代码
2023-03-02 12:15:35
Android定时器和倒计时实现淘宝秒杀功能
2023-01-18 02:17:04
![](https://img.aspxhome.com/file/2023/3/137153_0s.png)
java面向对象编程类的内聚性分析
2022-02-24 23:43:00
C#使用BackgroundWorker控件
2022-10-25 05:06:35
![](https://img.aspxhome.com/file/2023/0/129640_0s.jpg)