Repository navigation
Iterables
#185
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The methods of Kilo's
Optionalsclass are provided as a less verbose alternative to similar methods defined byjava.util.Optional. For example, where you might write something like this usingOptional:with
Optionalsit can be written as follows (similar to the null-coalescing or "Elvis" operator found in other languages):Whereas this code could be used to determine the length of an optional string:
it could be written more simply using
Optionalsas follows (similar to the safe call feature of other languages):Similarly, Kilo's
Iterablesclass offers a less complex alternative to the methods ofjava.util.stream.Stream. For example, where you might write something like this usingStream:it could be written as follows using
Iterables:Whereas this code could be used to transform list of strings to a list of string lengths:
it could be written more simply using
Iterablesas follows:For a slightly more involved example, consider this code that groups a list of strings by length and then collects the result to a sorted map of string length to string count:
With
Iterables, it could be written as follows:With the
Streamversion, you need to go to the end of the (very confusing) call chain to even see what typeresultwill be. With theIterablesapproach, it's immediately obvious thatresultwill be a sorted map of length values to size values.Another advantage of
Iterablesis that, unlike aStream, anIterablecan be traversed using a traditional for-each loop. Developers are not forced to choose between one paradigm or the other.For more information, see the README.
All reactions