By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

generators in Java?

P: n/a

This may have been discussed before, so I apologize.

Does Java have generators? I am aware of the "Iterator" interface,
but it seems much more restrictive. Python generators are useful
for many more things than simply list enumeration, but the Java
Iterator seems limited.

Tom
Dec 31 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tom Sheffler schrieb:

This may have been discussed before, so I apologize.

Does Java have generators? I am aware of the "Iterator" interface,
but it seems much more restrictive. Python generators are useful
for many more things than simply list enumeration, but the Java
Iterator seems limited.


No, it hasn't. One thing people do is to create threads that communicate
via a queue and block while that queue has an item in it. But of course
that comes at additional overhead of thread context switching.
Regards,

Diez
Dec 31 '05 #2

P: n/a
On Fri, 30 Dec 2005, Tom Sheffler wrote:
Does Java have generators? I am aware of the "Iterator" interface,
but it seems much more restrictive. Python generators are useful
for many more things than simply list enumeration, but the Java
Iterator seems limited.


What makes you think that Java's Iterators are more restrictive or
limited? As far as I understand, Java's hasNext/next protocol is
essentially the same as Python's next/StopIteration protocol. The main
advantage of the use of generators is that, when converting from a
callback-style or non-streaming function to one that produces values
on-demand, you don't have to rewrite your function to save intermediate
states (which can be a considerable amount of work).

A good explanation of the rationale for generators in Python is in the
"Motivation" section of PEP 255:

http://www.python.org/peps/pep-0255.html

--
.:[ dave benjamin: ramen/[spoomusic.com] ]:.
Jan 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.