Java Reference
In-Depth Information
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Override
public String toString() {
return firstName + " " + lastName + ", " + gender + ", " + dob;
}
// A utility method
public static List<Person> getPersons() {
ArrayList<Person> list = new ArrayList<>();
list.add(new Person("John", "Jacobs", LocalDate.of(1975, 1, 20), MALE));
list.add(new Person("Wally", "Inman", LocalDate.of(1965, 9, 12), MALE));
list.add(new Person("Donna", "Jacobs", LocalDate.of(1970, 9, 12), FEMALE));
return list;
}
}
The
FunctionUtil
class in Listing 5-13 is a utility class. Its methods apply a function on a
List. List
is an
interface that is implemented by the
ArrayList
class. The
forEach()
method applies an action on each item in the
list, typically producing side effects; the action is represented by a
Consumer
. The
filter()
method filters a list based
on a specified
Predicate
. The
map()
method maps each item in the list to a value using a
Function
. As a library
designer, you will design these methods using functional interfaces. Note that the
FunctionUtil
class contains no
mention of lambda expressions. You could have designed this class the same way even before Java 8.