Skip to content

Commit 6b629b7

Browse files
committed
Create Iterator vs loop for Collection
1 parent a5ea02f commit 6b629b7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Iterator vs loop for Collection

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// NOTE 1: Arrays.sort, Collections.sort etc use quicksort which is inplace sorting algo. Hence these methods are void
2+
/*
3+
* Note 2: While iterating over Collection, if we are making modification to Collection
4+
* then we sould use Iterator for iterating
5+
*
6+
* If we are not making any modification, then we can use get(int index) method
7+
* Example: List<Meeting> meetings = new ArrayList<Meeting>();
8+
* int count=0;
9+
* Meeting curr=null;
10+
* Meeting prev = meetings.get(0);
11+
* for(int i=1;i<meetings.size();i++){
12+
* curr=meetings.get(i);
13+
* if(curr.start < prev.end)
14+
* count++;
15+
* prev=curr;
16+
* }
17+
*
18+
Sources: http://stackoverflow.com/questions/5504570/iterator-vs-for-loop-and-why-iterator-was-introduced-as-we-had-for-loop
19+
http://stackoverflow.com/questions/2113216/which-is-more-efficient-a-for-each-loop-or-an-iterator
20+
*/

0 commit comments

Comments
 (0)