wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why it returns a new iterator object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why it returns a new iterator object " Watch "Why it returns a new iterator object " New topic
Author

Why it returns a new iterator object

Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
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

Joined: Jul 30, 2006
Posts: 138
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

Joined: Aug 28, 2006
Posts: 45
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

Joined: Aug 15, 2006
Posts: 165
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:


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Charith Fernando
Ranch Hand

Joined: Sep 12, 2005
Posts: 67
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..


Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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

Joined: Nov 01, 2006
Posts: 381

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.


~Sanjeev Singh<br />SCJP 1.5
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Thanks to All,

now the concept is clear to me.

Anvi

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why it returns a new iterator object
 
Similar Threads
mock exam example
Marcus Green Q=21 Collection
question about treeSet (K&B) Chapter 7 Question 3
Result of this code?