博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java8foreach_Java forEach – Java 8 forEach
阅读量:2534 次
发布时间:2019-05-11

本文共 4134 字,大约阅读时间需要 13 分钟。

java8foreach

Java forEach method was introduced in Iterable interface in . Java 8 forEach method is another way that we can use to traverse through a collection.

Java forEach方法是在 Iterable接口中引入的。 Java 8 forEach方法是我们可以用来遍历集合的另一种方法。

Java forEach (Java forEach)

Below code snippet shows the default implementation of java forEach method in Iterable interface. Read to learn more about default interface methods.

下面的代码片段显示了Iterable接口中java forEach方法的默认实现。 阅读以了解有关默认接口方法的更多信息。

default void forEach(Consumer
action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }

Java forEach method performs the given action for each element of the Iterable until all elements have been processed or exception is thrown.

Java forEach方法对Iterable的每个元素执行给定的操作,直到处理完所有元素或引发异常为止。

Java 8 forEach列表示例 (Java 8 forEach List Example)

Before java 8, We could iterate over a list by using for loop or .

在Java 8之前,我们可以使用for循环或遍历列表。

List
list = getList(); //prior to java 8//using enhanced for loopfor(String s : list){ System.out.println(s);}//using iteratorIterator
it = list.iterator();while(it.hasNext()){ System.out.println(it.next());}

Now same thing can be performed using java forEach method as shown below.

现在可以使用java forEach方法执行相同的操作,如下所示。

// Create Consumer instanceConsumer
action = new Consumer
(){ @Override public void accept(String t) { System.out.println(t); } };//java 8 forEachlist.forEach(action);

Since Consumer is a , we can use lambda expression and write above code in one line as;

由于Consumer是一个 ,因此我们可以使用lambda表达式并将上述代码写成一行;

list.forEach(k -> {System.out.println(k);});

Java 8 forEach Map示例 (Java 8 forEach Map Example)

Prior to java 8, we iterate over elements like below.

在Java 8之前,我们对元素进行如下迭代。

Map
map = getMap();//iteration prior to java 8Set
keySet = map.keySet();//using enhanced for loopfor (Integer i : keySet){ System.out.println(map.get(i));}//using iteratorIterator
it = keySet.iterator();while(it.hasNext()){ System.out.println(map.get(it.next()));}

Since Map doesn’t extend Iterable, forEach method is added into Map interface in java 8 and below shows the default implementation.

由于Map不扩展Iterable,因此java 8中的Map接口中添加了forEach方法,下面显示了默认实现。

default void forEach(BiConsumer
action) { Objects.requireNonNull(action); for (Map.Entry
entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } action.accept(k, v); } }

Let’s look at a simple example how we can use java 8 forEach with Map.

让我们看一个简单的示例,如何将Java 8 forEach与Map结合使用。

BiConsumer
action = new BiConsumer
(){ @Override public void accept(Integer t, String u) { System.out.println(u); } };//java 8 forEach with Mapmap.forEach(action);

Same code can be written using lambda expressions as below.

可以使用以下lambda表达式编写相同的代码。

map.forEach((k,v) -> {System.out.println(v);});

Java forEach的好处 (Java forEach Benefits)

I don’t see too much benefit of forEach loop except when you are using it with parallel stream. A new method was added in Collection interface to get the parallel stream.

我没有看到forEach循环有太多好处,除非将它与并行流一起使用。 在Collection接口中添加了一种新方法来获取并行流。

default Stream
parallelStream() { return StreamSupport.stream(spliterator(), true); }

So if we have to iterate over a collection and we are not bothered about sequential iteration, then we can use parallel stream with forEach loop as shown below.

因此,如果我们必须遍历一个集合,而不必担心顺序迭代,那么可以将并行流与forEach循环一起使用,如下所示。

//parallel operation using streamlist.parallelStream().forEach(action);

That’s all for Java forEach method. I hope you will find some use case for java 8 forEach method in parallel processing.

这就是Java forEach方法的全部内容。 希望您能在并行处理中找到Java 8 forEach方法的一些用例。

翻译自:

java8foreach

转载地址:http://nhlzd.baihongyu.com/

你可能感兴趣的文章
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>