我有一个List<Staff>
从数据库获取这些值的地方。我需要找到将是动态的特定字段的最大字符大小。现在我能够获得 firstName 的最大大小,但它的类型是硬编码的,如下所示。
public class Staff {
private String firstName;
private String lastName;
private String surName;
private String mailId;
}
List<String> fields = Arrays.asList("firstName", "lastName", "surName");
List<Staff> staffList = staffRepository.findAll();
fields.stream.map(field ->
Long maxLength = staffList.stream()
.map(Staff::getFirstName()) // i need to pass the field which will be dynamically changing
.mapToInt(String::length)
.max()).mapToLong(max -> max.orElse(0)).sum()