内部迭代的好处 [英] Benefits of internal iterations

查看:72
本文介绍了内部迭代的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道内部与外部迭代的真正好处是什么,以及为什么使用内部操作更好(至少是我所听到的). 在对集合进行内部迭代时,是否还可以删除集合的元素?就像在代码示例中一样:

I just wanted to know, what the real benefits of Internal vs External Iterations are and why it is better to use internal operations (that's what I heard at least). Is it also possible to delete elements of a collection while internally iterating over the collection? Like in the code example:

我知道内部迭代的代码可读性更好,但是还有其他诸如性能改进之类的好处吗?

I know that the code readability of internal iterations is better, but are there some other benefits like performance improvements?

//List with Strings of Fruit-Names
      Iterator i = aList.iterator();
      String str = "";
      while (i.hasNext()) {
         str = (String) i.next();
         if (str.equals("Orange")) {
            i.remove();
            System.out.println("\nThe element Orange is removed");
            break;
         }
      }

推荐答案

您的条件有些简单,因为您可以简单地使用aList.remove("Orange") resp. aList.removeAll(Collections.singleton("Orange"))代替,但是有一个内部迭代的替代方法,它也可以用于更复杂的条件aList.removeIf(str -> str.equals("Orange")).

Your condition is somewhat simplistic, as you could simply use aList.remove("Orange") resp. aList.removeAll(Collections.singleton("Orange")) instead, but there is an alternative with internal iteration which also works with more complex conditions, aList.removeIf(str -> str.equals("Orange")).

ArrayList的情况下,这将立即显示内部迭代的优势:在Iterator上调用remove()的情况下,ArrayList无法控制循环,因此不知道何时您退出它分别.放弃Iterator.您可以随时通过List界面访问列表,阅读并继续进行迭代或写入,而无需进一步迭代.

In case of ArrayList, this will immediately show the advantage of internal iteration: in case of calling remove() on an Iterator, the ArrayList has no control over the loop, hence doesn’t know when you exit it resp. abandon the Iterator. You can access the list through the List interface at any time, reading and continue iterating or writing and not iterating further.

因此,每次调用remove()时,都必须使列表进入一致状态,即,删除元素时,所有后续元素都必须在正确的位置复制.这使得从ArrayList迭代和删除O(n²)时间复杂度的最坏情况.

So every time you invoke remove(), the list has to be brought into a consistent state, i.e. all subsequent elements have to be copied at the right place when removing an element. This gives iterating and removing from an ArrayList a worst case of O(n²) time complexity.

相反,removeIf方法仅在返回时才需要提供List的完整状态.因此,它可以将复制元素推迟到最终位置已知的时候,这使它成为O(n)操作.因此,对于大型列表,存在明显的性能优势.

In contrast, the removeIf method only has to provide a completed state of the List when the method returns. Hence, it may postpone copying elements to the point when the final position is known, which makes it an O(n) operation. So, for large lists, there’s a significant performance advantage.

通常,具有内部迭代的方法提供了针对特定内部数据结构进行优化实现的可能性,而永远不会比外部循环更糟糕,因为基于迭代器的循环无论如何都是这些方法的后备.

Generally, methods with internal iterations provide the possibility of being implemented optimized for the particular internal data structure, while never being worse than the external loop, as the iterator based loop is the fallback of these methods anyway.

这篇关于内部迭代的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆