遍历数组1.0
public static void ergodicList(List list){
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
看一下编译出的字节码
遍历List1.5
public static void ergodicList(List list){
for (Integer temp : list) {
System.out.println(temp);
}
}
编译结果
遍历List 2.0
public static void ergodicList(List list){
Stream stream = list.stream();
// 使用consumer接口实现方法
Consumer consumer = new Consumer() {
@Override
public void accept(Integer temp) {
System.out.print(temp);
}
};
stream.forEach(consumer);
}
编译结果
遍历List 2.5
public static void ergodicList(List list){
Stream stream = list.stream();
Consumer consumer = (num) -> {
System.out.print(num);
};
stream.forEach(consumer);
}
编译结果
遍历List 3.0
public static void ergodicList(List list){
Stream stream = list.stream();
Consumer consumer = num -> System.out.print(num);
stream.forEach(consumer);
}
编译结果和2.5一样
遍历List 3.5
public static void ergodicList(List list){
Stream stream = list.stream();
stream.forEach(System.out::print);
}
编译结果
遍历List 4.0
public static void ergodicList(List list){
list.stream().forEach(System.out::print);
}
编译结果和3.5一样
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)