Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Python generator in Java?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think the answer to this is no, but I'm asking just to be sure...

Python has "generators" which are functions that remember where it left off so that every time you call the generator's "next()" function, you get the next object until you reach the end when it throws an exception. It's really handy for iterating over stuff without the overhead of having to keep track of where you are yourself. Is there something similar in Java?

What I'm trying to do is read from a database and iterate over the results of a query without having to return the actual resultSet itself. I want to keep the resultSet in my class and just keep returning the next row when it's requested. I'm assuming, without generators, that I can just put chunks of the result in a list and keep track of where I am and just keep refilling my list until I reach the end...but I'm lazy .

Thanks,

Rob
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has iterators (see interface Iterator). You can implement your own iterator to do the same thing as a generator in Python.

You could write a method that returns an Iterator over the result type.

Be aware that a ResultSet, as well as the JDBC Statement and Connection objects, need cleanup when you're done with it - you have to call close() on them to release resources when you're done.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also take care of what to return. Will it be a String[], an Object[], a Map<String,String>, a Map<String,Object>, or perhaps even instances of a class that will hold all the data.

There is one issue with creating an Iterator over a ResultSet - ResultSet's next() will return false when you're done, whereas Iterator's next() will throw an exception. It has the hasNext() method for querying whether or not to go on. ResultSet's next is a bit like a combination of Iterator's hasNext() and next() combined into one.

You may want to check out Apache Commons DbUtils, especially its ResultSetIterator.
 
Rob Marshall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestions. DbUtils looks interesting, I'll take a closer look.

Rob
 
Maybe he went home and went to bed. And took this tiny ad with him:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic