vendredi 1 mai 2020

Get Property that has max Character size from list of objects java

I have an List<Staff> where i will be getting those values from db. I need to find the max character size of a specific field which will be dynamic. For now im able to get the max size of firstName but its kind of hardcoded as shown below.

 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()




Aucun commentaire:

Enregistrer un commentaire