generators in Java?

T

Tom Sheffler

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
 
D

Diez B. Roggisch

Tom said:
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
 
D

Dave Benjamin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top