Skip to content

Commit

Permalink
Create Iterator vs loop for Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
nkatre committed Feb 26, 2015
1 parent a5ea02f commit 6b629b7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Iterator vs loop for Collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// NOTE 1: Arrays.sort, Collections.sort etc use quicksort which is inplace sorting algo. Hence these methods are void
/*
* Note 2: While iterating over Collection, if we are making modification to Collection
* then we sould use Iterator for iterating
*
* If we are not making any modification, then we can use get(int index) method
* Example: List<Meeting> meetings = new ArrayList<Meeting>();
* int count=0;
* Meeting curr=null;
* Meeting prev = meetings.get(0);
* for(int i=1;i<meetings.size();i++){
* curr=meetings.get(i);
* if(curr.start < prev.end)
* count++;
* prev=curr;
* }
*
Sources: http://stackoverflow.com/questions/5504570/iterator-vs-for-loop-and-why-iterator-was-introduced-as-we-had-for-loop
http://stackoverflow.com/questions/2113216/which-is-more-efficient-a-for-each-loop-or-an-iterator
*/

0 comments on commit 6b629b7

Please sign in to comment.