{"id":3681,"date":"2014-03-29T07:53:56","date_gmt":"2014-03-29T07:53:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/why-does-iterator-define-the-remove-operation-collection-of-common-programming-errors\/"},"modified":"2014-03-29T07:53:56","modified_gmt":"2014-03-29T07:53:56","slug":"why-does-iterator-define-the-remove-operation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/why-does-iterator-define-the-remove-operation-collection-of-common-programming-errors\/","title":{"rendered":"Why does Iterator define the remove() operation?-Collection of common programming errors"},"content":{"rendered":"<p>There are situations where you want to be able to remove elements using the iterator because it is the most efficient way to do it. For example, when traversing a linked data structure (e.g. a linked list), removing using the iterator is an <code>O(1)<\/code> operation &#8230; compared to <code>O(N)<\/code> via the <code>List.remove()<\/code> operations.<\/p>\n<p>And of course, <em>many<\/em> collections are designed so that modifying the collection during a collection by any other means than <code>Iterator.remove()<\/code> will result in a <code>ConcurrentModificationException<\/code>.<\/p>\n<p>If you have a situation where you don&#8217;t want to allow modification via a collection iterator, wrapping it using <code>Collection.unmodifiableXxxx<\/code> and using it&#8217;s iterator will have the desired effect. Alternatively, I think that Apache Commons provides a simple unmodifiable iterator wrapper.<\/p>\n<p>By the way <code>IEnumerable<\/code> suffers from the same &#8220;smell&#8221; as <code>Iterator<\/code>. Take a look at the <code>reset()<\/code> method. I was also curious as to how the C# <code>LinkedList<\/code> class deals with the <code>O(N)<\/code> remove problem. It appears that it does this by <em>exposing the internals of the list<\/em> &#8230; in the form of the <code>First<\/code> and <code>Last<\/code> properties whose values are <code>LinkedListNode<\/code> references. That violates another design principle &#8230; and is (IMO) far more dangerous than <code>Iterator.remove()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are situations where you want to be able to remove elements using the iterator because it is the most efficient way to do it. For example, when traversing a linked data structure (e.g. a linked list), removing using the iterator is an O(1) operation &#8230; compared to O(N) via the List.remove() operations. And of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3681","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3681"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3681\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}