Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet120/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   116   117   118   119   120   121   122   123   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Java 8 introduced new features that greatly increase developer productivity for sorting 
collections. Three features are demonstrated in the solution to this recipe: lambda 
expressions, method references, and streams. We look into streams in more detail within 
other recipes in this book, but we also briefly describe them here to help you understand 
this recipe. Streams can be applied to data collections, and they allow enhanced 
functional-style operations to be applied to the elements within the collections. Streams 
do not store any data; rather, they enable more functionality on the collections obtained 
from them.
In solution 1, a comparator is generated, by which the Player objects are evaluated 
for the number of goals scored (getGoals). A stream is then generated from a 
List
 that is referenced as a team. The stream provides the sorted() function, 
which accepts a comparator to perform a sort on a stream of data. The comparator that 
was initially generated is passed to the sorted() function, and then the map() function is 
called on the result. The map() function can apply an expression to each element within 
the stream. Therefore, within the map, this solution utilizes a lambda expression to 
create a string that contains each Player object’s firstName, lastName, and goals fields. 
Lastly, since List
 is iterable, it contains the forEach() method. The forEach() 
method enables an expression or group of statements to be applied to each element 
within the list. In this case, each element in the list is printed to the command line. Since 
the map() function was applied to the stream, each element in the list is subsequently 
printed per the algorithm applied within map(). The result is that the players’ first and 
last names and the number of goals each has scored are printed on the command line.
Solution 2 uses a different technique to accomplish a similar task. The Collections 
.sort() method is invoked on the list. The first argument to Collections.sort() is the 
list itself, and the second argument is the comparison implementation in the form of a 
lambda expression. The lambda expression, in this case, has two parameters passed to 
it, both Player objects, and it compares the last name of the first player to the last name 
of the second player. Therefore, the sort is performed on the lastName field of the Player 
object, in ascending order. To finish solution 2, the sorted list is printed out. To do this, a 
stream is generated from the sorted list, and the forEach() method is then invoked on 
the stream of data, printing out each player’s last name.
ChapTer 6 LaMbda expressIons


223

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   116   117   118   119   120   121   122   123   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling