Java 17 Recipes


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

 How It Works
Consider that your lambda expression will invoke a single method by name, perhaps 
returning a result. If a lambda expression fits this scenario, it is a prime candidate for 
use with a method reference. A method reference is a simplified form of a lambda 
expression, which specifies the class name or instance name, followed by the method to 
be called in the following format.
::
The double colon (::) operator specifies a method reference. Since a method 
reference is a simplified lambda method, it must implement a functional interface. 
The abstract method within the interface must have the same argument list and return 
type as the method being referenced. Any arguments are subsequently derived from 
the context of the method reference. For instance, consider the same scenario as the 
solution, whereby you wanted to sort an array of Player objects by calling on the 
Player.compareByGoal() method to perform goal comparisons. The following code 
could enable this functionality via a lambda expression.
Arrays.sort(teamArray, (p1, p2) -> Player.compareByGoal(p1,p2));
In this code, the array is passed as the first argument to Arrays.sort().
The second argument is a lambda expression that passes two Player objects to the
Player.compareByGoal() method. The lambda expression uses the functional interface 
Comparator
.compare, which utilizes the (Player, Player) parameter list. The 
compareByGoal() method contains that same parameter list. Likewise, the return type 
of compareByGoal() matches the return type within the functional interface. Therefore, 
the parameter list does not need to be specified in the listing. It can be inferred from the 
context of the Player::compareByGoal method reference instead.
ChapTer 6 LaMbda expressIons


217
There are four different types of method references. Table 
6-1
 lists each of them.
In the solution, the static method reference type is demonstrated since 
compareByGoal() is a static method within the Player class. It is possible to invoke a 
method of an object instance using an instance reference. Consider the following class
which contains a nonstatic method for comparing goals within Player objects.
public class PlayerUtility {
public int compareByGoal(Player a, Player b){
int eval;
if(a.getGoals() > b.getGoals()){
eval = 1;
} else if (a.getGoals() < b.getGoals()){
eval = -1;
} else {
eval = 0;
}
return eval;
}
}
This class can be instantiated, and the new instance references the 
compareByGoals() method, similar to the technique used in the solution to this recipe.
Player[] teamArray2 = team.toArray(new Player[team.size()]);
PlayerUtility utility = new PlayerUtility();
Arrays.sort(teamArray2, utility::compareByGoal);

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   113   114   115   116   117   118   119   120   ...   245




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