tag:blogger.com,1999:blog-7795931692371174013.post2913389970054823412..comments2017-08-17T06:37:57.785-07:00Comments on Java d'eau: The Way of the Lambda - Elegant and Efficient Dan Lawessonhttp://www.blogger.com/profile/12417263510113411435noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-7795931692371174013.post-84124594065762345412017-08-06T04:32:15.358-07:002017-08-06T04:32:15.358-07:00Admiring the time and effort you put into your blo...Admiring the time and effort you put into your blog and detailed information you offer!..<br /><a href="http://kupongnytt.se" rel="nofollow">Lyko Rabattkod</a>Robert F. Crockerhttps://www.blogger.com/profile/12114479635982380752noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-697028969736062132017-07-07T01:12:12.536-07:002017-07-07T01:12:12.536-07:00Nice post. I also want you to visit this web-page ...Nice post. I also want you to visit this web-page to learn more about <a href="https://besttrackingapps.com/facebook-spy-apps/" rel="nofollow">facebook spy app</a>. reginald suricthttps://www.blogger.com/profile/02865395258495641241noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-40130721369864876092017-04-18T02:20:49.537-07:002017-04-18T02:20:49.537-07:00This comment has been removed by a blog administrator.Jack Sonhttps://www.blogger.com/profile/11169326970593595904noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-23405399349969444342016-10-26T07:36:12.516-07:002016-10-26T07:36:12.516-07:00As written above, the execution will take place in...As written above, the execution will take place in a single thread. It is how ever trivial to add a ".parallel()" to the stream to make the solution using several threads. When happens then is that the stream is split into chunks of data, to be handled in parallel streams created transparently to the user. <br /><br />Good question, BTW, in some future blog post I will use this as an example of the advantages of declarative programming. Dan Lawessonhttps://www.blogger.com/profile/12417263510113411435noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-75873231093115328872016-10-24T12:07:47.220-07:002016-10-24T12:07:47.220-07:00Does the Java 8 solution silently use multithreadi...Does the Java 8 solution silently use multithreading ?<br />Or can it be modifier for this ?<br />Ludovic Verleyhttps://www.blogger.com/profile/11693349258065864170noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-10488933998297439292016-10-21T19:54:04.008-07:002016-10-21T19:54:04.008-07:00Backwards is faster but still O(n^2). I agree that...Backwards is faster but still O(n^2). I agree that the cost of removal is highly dependent on the data structure used. Contrasting with a LinkedList in the beginning, this blog post is intended to cover only the special case of an ArrayList.Dan Lawessonhttps://www.blogger.com/profile/12417263510113411435noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-48881494269644367072016-10-21T18:33:21.410-07:002016-10-21T18:33:21.410-07:00Initialize your i to the arrayList.size()-1 and de...Initialize your i to the arrayList.size()-1 and decrement. That will be faster.Karl the Paganhttps://www.blogger.com/profile/08670068559230178483noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-10623289436942050232016-10-21T08:09:18.591-07:002016-10-21T08:09:18.591-07:00The cost to remove a item from a Collection depend...The cost to remove a item from a Collection depends on the implementation.<br />The most famous being Arraylist O(N) and LinkedList O(1)Marcelo Carabolantehttps://www.blogger.com/profile/04606396302240336570noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-77906371770361042692016-10-16T14:12:04.664-07:002016-10-16T14:12:04.664-07:00I did not intend to convey that I have created any...I did not intend to convey that I have created any novel solution to the removal problem, on the contrary I meant to point out that the Java 8 remove operation is O(n). Thus, none of the solutions are mine in any sense.<br /><br />I think that your comment beautifully illustrates the reason why the The Java 8 implementation is more efficient. Ignoring any language or implementation details and just considering the problem per se, it is easy to see that removal of elements in an array is O(n) (since it entails relocation of O(n) elements). Thus, solving the general problem of removing n elements from an array by using the basic removal operation will always be O(n^2). <br /><br />The reason for this is that we have introduced an extra constraint to the original problem when deciding to use item removal operations. The basic item removal operation has to maintain the data structure invariants for each loop of our solution, which (as you point out) requires relocation of all elements of higher indices.<br /><br />Your suggestion demonstrates a straight-forward solution to the problem - just copy the data over to a new structure while figuring out the new position of the element. The Java 8 removeIf solution is slightly more complex but avoids creating a new List. (Basically two passes - one to find the elements to move and a second to move each element once to its new position.)<br /><br />See https://gist.github.com/lawesson/101ed054fcdb00a94d69b34e04ef0be7Dan Lawessonhttps://www.blogger.com/profile/12417263510113411435noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-3799030216274810432016-10-16T12:50:58.565-07:002016-10-16T12:50:58.565-07:00That is also O(n^2). The loop will pass n times, a...That is also O(n^2). The loop will pass n times, and each remove operation is O(n), so we end up with an O(n*n) solution. Dan Lawessonhttps://www.blogger.com/profile/12417263510113411435noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-86081812637802557852016-10-16T02:14:15.829-07:002016-10-16T02:14:15.829-07:00Your solutions also works in O(n^2) time - after e...Your solutions also works in O(n^2) time - after each removal all elements in ArrayList will be shifted in memory.<br />One of the easiest solutions without Java Streams is to create new list only with elements satisfying predicate:<br /><br />List filteredItems = new ArrayList<>();<br />for (Item item: items) {<br /> if (predicate(item)) {<br /> filteredItems.add(item);<br /> }<br />}PaweÅ‚ Michalakhttps://www.blogger.com/profile/18119284845069550626noreply@blogger.comtag:blogger.com,1999:blog-7795931692371174013.post-61526250643085503422016-10-16T00:21:43.179-07:002016-10-16T00:21:43.179-07:00How does it compare with an index loop?
for (int i...How does it compare with an index loop?<br />for (int i = 0; i < arrayList.size(); i++) {<br />if (condition) arrayList.remove(i--);<br />}coladicthttps://www.blogger.com/profile/05747512533360463523noreply@blogger.com