I have a UserProfile
class which contains user\'s data as shown below:
class UserProfile {
private String userId;
private String displayNam
You can simply a lot your code by creating a Stream over the given list of functions:
public static <T> long getNonNullFieldCount(T t, List<Function<? super T, ?>> functionList) {
return functionList.stream().map(f -> f.apply(t)).filter(Objects::nonNull).count();
}
This will return the count of non-null
fields returned by each function. Each function is mapped to the result of applying it to the given object and null
fields are filtered out with the predicate Objects::nonNull.