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
Comparison of C Sharp and Java
(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!
====Using a foreach==== The same example above, but using a method returning an Iterable to maintain greater similarity with the C# example. Anything that implements the iterable interface can be iterated in a foreach. <syntaxhighlight lang="java" style="font-size:90%"> Iterable<Integer> fibonacci(int limit) { return Stream.generate(new Supplier<Integer>() { int a = 0; int b = 1; public Integer get() { int temp = a; a = b; b = a + temp; return temp; } }).limit(limit)::iterator; } </syntaxhighlight> <syntaxhighlight lang="java" style="font-size:90%"> // print the 10 first Fibonacci numbers for(int it: fibonacci(10)) { System.out.println(it); } </syntaxhighlight> The most common way to do the example above would be to use Streams, not Iterables. This could be returned from a method like the C# example, but it's unnecessary and could be used directly by just collecting the Stream. Below is an example using Streams and the collecting the Stream calling {{mono|toList}} in the foreach block. <syntaxhighlight lang="java" style="font-size:90%"> var fibonacci = Stream.generate(new Supplier<Integer>() { int a = 0; int b = 1; public Integer get() { int temp = a; a = b; b = a + temp; return temp; } }); </syntaxhighlight> <syntaxhighlight lang="java" style="font-size:90%"> // print the 10 first Fibonacci numbers for(int it: fibonacci.limit(10).toList()) { System.out.println(it); } </syntaxhighlight> In addition to the toList collector in the foreach block, It's important to highlight that there are more collectors for every type of collection. Also, custom collectors could be created by implementing the {{mono|Collector}} interface or describing the implementation as a lambda expression, both cases passing it as arguments to the collect method of the {{mono|Stream}} object. In this example would be just calling the {{mono|collect}} method instead {{mono|toList}} if would have some complex type of object per item for the collection. Both examples could also be done with {{mono|IntStream}} and {{mono|IntSupplier}} and avoid the {{mono|Integer}} generic in the {{mono|Supplier}} interface implementation, but the generic is used to preserve greater similarity with the C# example.
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)