I'm fairly new to generics here and would like some assistance.
I'm trying to create a Generic method with a Generic Predicate
, a Generic java.util Function
, a Generic List
as arguments. The method is like this:
public static <T> T getValue(Predicate<? super Object> condition, Function<? super Object, ? extends Object> mapper, T elseResult, List<T> table) {
T result = null;
if (table != null)
result = table.stream()
.filter(condition)
.map(mapper).findAny().orElse(elseResult); // Syntax error here.
else
result = elseResult;
return (T) result;
}
I'm getting an error on the orElse(elseResult)
method. This is the error -
The method orElse(capture#1-of ? extends Object) in the type Optional<capture#1-of ? extends Object> is not applicable for the arguments (T).
I'm not exactly sure as to what this error is about. So could someone tell me what I'm doing wrong here? Thanks.