我正在尝试查找具有给定java属性的最大长度的字符串。我会将属性名作为字符串传递给该方法,该方法将返回最大长度的字符串值。
class Employee {
private String name;
private String designation;
private List<Address> address;
private ContactInfo contactInfo;
....
getter setter
}
class Address {
private String city;
private String state;
private String country;
......
getter setter
}
class ContactInfo {
private String mobileNumber;
private String landlineNumber;
....
getter setter
}我有一些数据,如下所示:
ContactInfo contactInfo = new ContactInfo("84883838", "12882882");
Address address1 = new Address("city111", "state111", "country111");
Address address2 = new Address("city111111", "state11112", "country1112");
Employee employee1 = new Employee("xyz", "uyyy", List.of(address1, address2), contactInfo);
private String findStringWithMaxLength(String attribute) {
return employeeList.stream()
....
}在上面的例子中,如果我以"city“的形式提供属性值,那么由于字符串的最大长度,它应该返回值"city111111”。
如果我们有子对象和对象列表,我该如何使用给定的属性遍历。
发布于 2021-09-02 07:54:15
您可以创建一个接受雇员列表的方法和一个获取特定属性的函数,如下所示:
private String findStringWithMaxLength(List<Employee> employees, Function<Employee, String> function) {
return employees.stream()
.map(function)
.max(Comparator.comparing(String::length))
.orElseThrow(() -> new IllegalArgumentException("Empty list"));
}调用你可以使用的方法:
findStringWithMaxLength(employees, Employee::getName)
findStringWithMaxLength(employees, Employee::getDesignation)
findStringWithMaxLength(employees, Employee::getAddress)请注意,如果列表为空,该方法将抛出异常,如果您不抛出异常,则可以用orElse(withDefaultValue)替换它
发布于 2021-09-02 07:55:32
你可以使用反射来做这件事,但是这里有一个更好的“类型安全”方法。
让这个类:
@Getter
@Setter
@AllArgsConstructor
static class Employee {
private String name;
private String designation;
private String address;
}使用getters,并让列表
static List<Employee> employeeList = asList(
new Employee("xyz1", "abc1234", "address 123"),
new Employee("xyz123", "abc123", "address 1234"),
new Employee("xyz1234", "abc12", "address 12")
);然后,您可以定义一个能够遍历任何String字段的泛型函数
static Optional<String> findStringWithMaxLength(Function<Employee, String> getter) {
return employeeList.stream().map(getter).max(Comparator.comparingInt(String::length));
}现在,您可以将每个getter应用于该函数
for(Function<Employee, String> getter: Arrays.<Function<Employee, String>>asList(
Employee::getName,
Employee::getDesignation,
Employee::getAddress))
System.out.println(findStringWithMaxLength(getter));带输出
Optional[xyz1234]
Optional[abc1234]
Optional[address 1234]( optional是必需的,因为列表可能为空)。
发布于 2021-09-02 08:21:19
给定的答案工作正常。在这种情况下,我想使用枚举。如果Employee类中的方法发生更改,则只需更改枚举,而不是每次使用它的调用:
enum EmployeeField {
NAME(Employee::getName),
DESIGNATION(Employee::getDesignation),
ADDRESS(Employee::getAddress);
private final Function<Employee, String> getter;
EmployeeField(Function<Employee, String> getter) {
this.getter = getter;
}
public Function<Employee, String> getGetter() {
return getter;
}
}
private static final List<Employee> employeeList = Arrays.asList(
new Employee("xyz1", "abc", "address 1"),
new Employee("xyz123", "abc", "address 1"),
new Employee("xyz1234", "abc", "address 1")
);
public static void main(String[] args) {
Optional<String> longestName = findStringWithMaxLength(EmployeeField.NAME);
if (longestName.isPresent()) {
System.out.println("Longest name = " + longestName.get());
} else {
System.out.println("No longest name");
}
}
private static Optional<String> findStringWithMaxLength(EmployeeField employeeField) {
return employeeList.stream()
.map(employee -> employeeField.getGetter().apply(employee))
.max(Comparator.comparing(String::length));
}为您所在城市的用例编辑,如下所示:
在与AddressField相同的模型上添加枚举EmployeeField
enum AddressField {
CITY(Address::getCity);
....
}然后添加一个方法
private static Optional<String> findStringWithMaxLength(List<Address> addressList, AddressField addressField) {
return addressList.stream()
.map(employee -> addressField.getGetter().apply(employee))
.max(Comparator.comparing(String::length));
}然后将城市枚举添加到EmployeeField枚举中:
LANDLINE_NUMBER(employee -> employee.getContactInfo().getLandlineNumber()),
CITY(employee -> findStringWithMaxLength(employee.getAddress(), AddressField.CITY).get());https://stackoverflow.com/questions/69026070
复制相似问题