I have a List called "racers" of simple class
class Racer {
private String name;
private String teamName;
// and getters
}
I am using а method to find maximum lengths of the fields in the list:
int maxRacerNameLength = getMaxFieldLength(racers, Racer::getName);
int maxTeamNameLength = getMaxFieldLength(racers, Racer::getTeamName);
Method implementation:
private int getMaxFieldLength(List<Racer> racers, Function<Racer, String> getter) {
return racers.stream().mapToInt(racer -> getter.apply(racer).length()).max().getAsInt();
}
How do I get rid of this last lambda using method reference?