generators in Java?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Sheffler

    #1

    generators in Java?


    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
  • Diez B. Roggisch

    #2
    Re: generators in Java?

    Tom Sheffler schrieb:[color=blue]
    >
    > 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.[/color]

    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

    Comment

    • Dave Benjamin

      #3
      Re: generators in Java?

      On Fri, 30 Dec 2005, Tom Sheffler wrote:
      [color=blue]
      > 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.[/color]

      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:

      This PEP introduces the concept of generators to Python, as well as a new statement used in conjunction with them, the yield statement.


      --
      .:[ dave benjamin: ramen/[spoomusic.com] ]:.

      Comment

      Working...