Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why it returns a new iterator object

 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Please go through this code which i got from one mock exam test.

Set set = new TreeSet();
set.add("F");
set.add("B");
set.add("E");
set.add("A");
int count=0;
while(set.iterator().hasNext()){
System.out.print(set.iterator().next()+" ");
count++;
if(count==3)break;
}
1) F B E
2) A B E
3) A B E F
4) F E B
5) none of the above

here the answer is A A A. The explanation for this is when the iterator is requested from the TreeSet it returns a new iterator
object which you try to iterate. which means each time the loop runs it searches in a new iterator object.
What is the meaning of the above and also why does it happens and how with example?

thanks in advance
Anvi
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because, every time you say set.iterator() , a new iterator object is created and the set.iterator().next() moves to the next element of the new iterator thats created just before it and not of the iterator thats in the while condition.
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Praveen

But still i have the confusion.
According to me the code is functioning as below:
In the set we have {F,B,E,A}, and when the count is 0 set.iterator will print "F" , count is then incremented to 1 then it prints "B", count incremented to 2 then prints "E", count is incremented to 3 then it won't print. since it is TreeSet we will have the natural order as "BEF".

Please correct me .
Thanks
Anvi
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each time you call set.iterator() you get a brand new iterator object that starts at the beginning, which is why it never advances beyond the first element ("A"). Compare with:
 
Charith Fernando
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me add something here since i was the one who posted that question...

the target of that question was to check the knowledge of TreeSet Collection class and Object referencing...

as Matt said, when it takes the iterator it returns a new Object of the iterator therefore everytime you ask for it, it starts from the begining... hope that is clear...

and the 2nd part is whether you know the characteristics of a TreeSet...

when elements are added into a TreeSet it gets ordered according to the natural order as Anvi said... so the first element of the Set would be "A"

and since we dont retrieve the elements by index which is not possible with "SET"s we could only access the first element..
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anvi Dixit:
Thanks Praveen

But still i have the confusion.
According to me the code is functioning as below:
In the set we have {F,B,E,A}, and when the count is 0 set.iterator will print "F" , count is then incremented to 1 then it prints "B", count incremented to 2 then prints "E", count is incremented to 3 then it won't print. since it is TreeSet we will have the natural order as "BEF".

Please correct me .
Thanks
Anvi


Hi Anvi,

The code results in a infinite loop because, an iterator stops when the last element is reached but as a new iterator is created every time, the code will never reach the last element. Thats why the count is restricting the loop. Also, the code will always print 'A' irrespective of the count value.Ofcourse, when the count is 0 it results in an infinite loop again. Hope this is clear.

Regards,
Praveen
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Line 1-5
Since the set is of type TreeSet all the entries added will be ordered and hence sorted naturally.so the set will be {A.B,E,F}
Line 7 : set.iterator() is creating a new object of iterator,subsequent call of hasNext() will return true every time.So the loop 7-11 is infinite loop(till the break breaks it.
Line 8: Again set.iterator() causes a new iterator object creation and subsequent call to next() will return 'A'(is the first element in the sorted Set)
Line 10: The loop will break after printing three A,when i will be 3.

Hope will help.
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to All,

now the concept is clear to me.

Anvi

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic