Java 17 Recipes


-4. Sorting with Fewer Lines of Code


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

6-4. Sorting with Fewer Lines of Code
 Problem
Your application contains a list of Player objects for a hockey team. You want to sort that 
list of players by those who scored the most goals, and you want to do so using terse yet 
easy-to-follow code.
 Solution 1
Create a comparator using an accessor method contained within the Player object for 
the field you want to sort. In this case, you want to sort by the number of goals, so the 
comparator should be based on the value returned from getGoals(). The following line 
of code shows how to create such a comparator using the Comparator interface and a 
method reference.
public static void main(String[] args) {
loadTeam();
Comparator
byGoals =
Comparator.comparing(Player::getGoals);
Next, utilize a mixture of lambda expressions and streams (see Chapter 
7
 for more 
information on streams), along with the forEach() method, to apply the specified sort 
on the list of Player objects. In the following line of code, a stream is obtained from the 
list, which allows you to apply functional-style operations on the elements.
ChapTer 6 LaMbda expressIons


220
team.stream().sorted(byGoals)
.map(p -> p.getFirstName() + " " + p.getLastName() + " - "
+ p.getGoals())
.forEach(element -> System.out.println(element));
Assuming that the list referenced by team is loaded with Player objects, the previous 
line of code first sorts that list by the player goals and then prints information on each object.
The following are the results of the sort.
== Sort by Number of Goals ==
Jonathan Gennick - 1
Josh Juneau - 5
Steve Adams - 7
Duke Java - 15
Bob Smith - 18
 Solution 2
Utilize the Collections.sort() method, passing the list to sort along with a lambda 
expression that performs the comparisons on the list elements. The following code 
demonstrates how to accomplish this task using the Collections.sort() technique.
public static void main(String[] args) {
loadTeam();
Collections.sort(team, (p1, p2)
-> p1.getLastName().compareTo(p2.getLastName()));
team.stream().forEach((p) -> {
System.out.println(p.getLastName());
});
The following is the result.
== Sort by Last Name ==
Adams
Gennick
Java
Juneau
Smith
ChapTer 6 LaMbda expressIons


221
Where the function loadTeam is:
public class Sorter {
static List
team;
private static void loadTeam() {
System.out.println("Loading team...");
team = new ArrayList();
Player player1 = new Player();
player1.setFirstName("Josh");
player1.setLastName("Juneau");
player1.setGoals(5);
Player player2 = new Player();
player2.setFirstName("Duke");
player2.setLastName("Java");
player2.setGoals(15);
Player player3 = new Player();
player3.setFirstName("Jonathan");
player3.setLastName("Gennick");
player3.setGoals(1);
Player player4 = new Player();
player4.setFirstName("Bob");
player4.setLastName("Smith");
player4.setGoals(18);
Player player5 = new Player();
player5.setFirstName("Steve");
player5.setLastName("Adams");
player5.setGoals(7);
team.add(player1);
team.add(player2);
team.add(player3);
team.add(player4);
team.add(player5);
}
...
}
ChapTer 6 LaMbda expressIons


222

Download 3.2 Mb.

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




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