public static void main(String[] args) throws Exception {
List list = Arrays.asList(“a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “d”, “f”, “f”, “g”);
list.forEach(str -> System.out.print(str + " "));
System.out.println();
}
运行结果:
那如果我们在遍历的时候需要使用到元素的索引呢(类似 getElementPositions
方法那样)?
很可惜,Java8
的 Iterable
并没有提供一个带索引的 forEach
方法。不过自己动手,丰衣足食——让我们自己写一个带索引的 forEach
方法:
import java.util.Objects;
import java.util.function.BiConsumer;
/**
- Iterable 的工具类
*/
public class Iterables {
public static void forEach(
Iterable<? extends E> elements, BiConsumer<Integer, ? super E> action) {
Objects.requireNonNull(elements);
Objects.requireNonNull(action);
int index = 0;
for (E element : elements) {
action.accept(index++, element);
}
}
}
该 forEach
方法第一个参数为要遍历的 Iterable
,第二个参数为 BiConsumer
。BiConsumer
的输入参数第一个即索引,第二个为元素。
我们测试下这个 forEach
方法:
public static void main(String[] args) throws Exception {
List list = Arrays.asList(“a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “d”, “f”, “f”, “g”);
Iterables.forEach(list, (index, str) -> System.out.println(index + " -> " + str));
}
运行结果:
结果和预期的一致。
现在我们使用 Iterables.forEach
改写 getElementPositions
方法:
public static Map<String, List> getElementPositions(List list) {
Map<String, List> positionsMap = new HashMap<>();
Iterables.forEach(list, (index, str) -> {
positionsMap.computeIfAbsent(str, k -> new ArrayList<>(1)).add(index);
});
return positionsMap;
}
public static void main(String[] args) throws Exception {
List list = Arrays.asList(“a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “d”, “f”, “f”, “g”);
System.out.println(“使用 computeIfAbsent 和 Iterable.forEach:”);
Map<String, List> elementPositions = getElementPositions(list);
System.out.println(elementPositions);
}
运行结果和原来一致:
真的不明白这么简单且实用的方法,Java8
为什么不在 Iterable
中提供一个默认实现(此处应有黑人问号)。
原文作者:mizhoux
原文地址:https://segmentfault.com/a/1190000007881498
大厂笔试内容集合(内有详细解析) 持续更新中…
欢迎关注个人微信公众号:Coder编程
欢迎关注Coder编程公众号,主要分享数据结构与算法、Java相关知识体系、框架知识及原理、Spring全家桶、微服务项目实战、DevOps实践之路、每日一篇互联网大厂面试或笔试题以及PMP项目管理知识等。更多精彩内容正在路上~
新建了一个qq群:315211365,欢迎大家进群交流一起学习。谢谢了!也可以介绍给身边有需要的朋友。
文章收录至
Github: https://github.com/CoderMerlin/coder-programming
Gitee: https://gitee.com/573059382/coder-programming