Java8中用foreach循环获取对象的index下标详解
前言
在Java8中,我们经常使用lambada表达式进行foreach循环,但是常常我们在遍历List的时候想获取对象的index,但是Java8、9、10、11都没有相关的支持,同样的问题也存在于增强型for循环中,很多时候不得不含着泪以 for (int i = 0; i < list.size(); i++) 的方式写代码
我们的期望
list.foreach((item,index)->{}) //编译不通过
常见的list获取index方法
for(int i=0;i<list.size();i++>)
for (int i = 0; i < list.size(); i++) { }
indexOf(Obj)
for (Object o : list) { list.indexOf(o); //如果是Set还没有这个方法 }
还有…
int i = 0; for (String s : list) { i++; }
很显然上述的方法并不是我们所想要的
Consumer和BiConsumer
我们看个简单的例子
Consumer<String> consumer = t -> System.out.println(t); consumer.accept("single"); BiConsumer<String, String> biConsumer = (k, v) -> System.out.println(k+":"+v); biConsumer.accept("multipart","double params");
输出结果:
single
multipart:double params
这里不难发现我们平时写的箭头函数其实是一个Consumer或者BiConsumer对象
定制Consumer
foreach源码
default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }
分析源码可知,我们的list foreach方法传入的是Consumer对象,支持一个参数,而我们想要的是item,index两个参数,很明显不满足,这时我们可以自定义一个Consumer,传参是BiConsumer,这样就能满足我们需求了,代码如下:
import java.util.ArrayList; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Consumer; public class LambadaTools { /** * 利用BiConsumer实现foreach循环支持index * * @param biConsumer * @param <T> * @return */ public static <T> Consumer<T> forEachWithIndex(BiConsumer<T, Integer> biConsumer) { /*这里说明一下,我们每次传入forEach都是一个重新实例化的Consumer对象,在lambada表达式中我们无法对int进行++操作, 我们模拟AtomicInteger对象,写个getAndIncrement方法,不能直接使用AtomicInteger哦*/ class IncrementInt{ int i = 0; public int getAndIncrement(){ return i++; } } IncrementInt incrementInt = new IncrementInt(); return t -> biConsumer.accept(t, incrementInt.getAndIncrement()); } }
调用示例:
List<String> list = new ArrayList(); list.add("111"); list.add("222"); list.add("333"); list.forEach(LambadaTools.forEachWithIndex((item, index) -> { System.out.println(index +":"+ item); }));
输出结果如下:
0:111
1:222
2:333
PS:这个Set也可以用哦,不过在Set使用中这个index可不是下标
总结
到此这篇关于Java8中用foreach循环获取对象的index下标的文章就介绍到这了,更多相关Java8获取对象index下标内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
学会IDEA REST Client后就可以丢掉postman了
这篇文章主要介绍了学会IDEA REST Client后就可以丢掉postman了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12maven <repositories>标签和<pluginRepositories>标签的使用
这篇文章主要介绍了maven <repositories>标签和<pluginRepositories>标签的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07Java日期时间类(Date、DateFormat、Calendar)解析
这篇文章主要介绍了Java日期时间类(Date、DateFormat、Calendar)解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-06-06
最新评论