Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generator (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Java=== Java has had a standard interface for implementing iterators since its early days, and since Java 5, the "foreach" construction makes it easy to loop over objects that provide the <code>java.lang.Iterable</code> interface. (The [[Java collections framework]] and other collections frameworks, typically provide iterators for all collections.) <syntaxhighlight lang="java"> record Pair(int a, int b) {}; Iterable<Integer> myIterable = Stream.iterate(new Pair(1, 1), p -> new Pair(p.b, p.a + p.b)) .limit(10) .map(p -> p.a)::iterator; myIterable.forEach(System.out::println); </syntaxhighlight> Or get an Iterator from the '''Java 8''' super-interface BaseStream of Stream interface. <syntaxhighlight lang="java"> record Pair(int a, int b) {}; // Save the iterator of a stream that generates fib sequence Iterator<Integer> myGenerator = Stream // Generates Fib sequence .iterate(new Pair(1, 1), p -> new Pair(p.b, p.a + p.b)) .map(p -> p.a).iterator(); // Print the first 5 elements for (int i = 0; i < 5; i++) { System.out.println(myGenerator.next()); } System.out.println("done with first iteration"); // Print the next 5 elements for (int i = 0; i < 5; i++) { System.out.println(myGenerator.next()); } </syntaxhighlight> Output: <syntaxhighlight lang="console"> 1 1 2 3 5 done with first iteration 8 13 21 34 55 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)