There’s a lot more to the collections library changes than I covered in Chapter 3. It’s time to cover some of the more advanced collections
changes, including the new
Collector abstraction. I’ll also introduce method
references, which are a way of using existing code in lambda expressions with
little to no ceremony. They pay huge dividends when it comes to writing
Collection-heavy code. More advanced topics within the collections library
will also be covered, such as element ordering within streams and other useful API
A common idiom you may have noticed is the creation of a lambda expression that calls a method on its parameter. If we want a lambda expression that gets the name of an artist, we would write the following:
This is such a common idiom that there’s actually an abbreviated syntax for this that lets you reuse an existing method, called a method reference. If we were to write the previous lambda expression using a method reference, it would look like this:
The standard form is
Classname::methodName. Remember that even though it’s a
method, you don’t need to use brackets because you’re not actually calling the
method. You’re providing the equivalent of a lambda expression that can be
called in order to call the method. You can use method references in the same
places as lambda expressions.
You can also call constructors using the same abbreviated syntax. If you ...