Java 17 Recipes


Table 6-1. Method Reference Types Type


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

Table 6-1. Method Reference Types
Type
Description
static reference
Uses a static method of an object
Instance reference
Uses an instance method of an object
arbitrary object 
Method
Used on an arbitrary object of a particular type, rather than a particular 
object
Constructor reference Generates a new object by invoking a constructor with the new keyword
ChapTer 6 LaMbda expressIons


218
Suppose that your application contained a list of an arbitrary type, and you want to 
apply a method to each of the objects in that list. Method references can be used in this 
scenario, given the object contains methods that are candidates for use via reference. 
In the following example, the Arrays.sort() method is applied to a list of int values. A 
method reference applies the Integer compare() method to the elements within the 
list. Thus, the resulting list is sorted, and the method reference automatically passes the 
int arguments and returns the int comparison.
Integer[] ints = {3,5,7,8,51,33,1};
Arrays.sort(ints, Integer::compare);
The last type of method reference can be utilized for referencing the constructor of 
an object. This type of method reference can be especially useful when creating new 
objects via a factory. Let’s take a look at an example. Suppose that the Player object 
contained the following constructor.
public Player(String position, int status, String first, String last){
this.position = position;
this.status = status;
this.firstName = first;
this.lastName = last;
}
You want to generate Player objects on the fly using a factory pattern. The 
following code demonstrates an example of a functional interface containing a single 
abstract method named createPlayer(), which accepts the same argument list as the 
constructor for the Player object.
@FunctionalInterface
public interface PlayerFactory {
Player createPlayer(String position,
int status,
String firstName,
String lastName);
}
The factory can now be created from a lambda expression and then created new 
objects. The following lines of code demonstrate.
ChapTer 6 LaMbda expressIons


219
PlayerFactory player1 = Player::new;
Player newPlayer = player1.createPlayer("CENTER", 0, "Constructor", 
"Referenceson");
The double colon operator (::), introduced in Java 8, simplifies to create a new 
instance. Method references were perhaps one of the most significant new features 
introduced in Java 8, although lambda expressions have more use cases. They provide an 
easy-to-read, simplified technique for generating lambda expressions, and they’ll work 
in most cases where lambda is merely invoking a single method by name.

Download 3.2 Mb.

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




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