How do I find the smallest value in a List<E>?

Viewed 52

I tried it this way but all I get is this Error Message: java: incompatible types: E cannot be converted to int

public static<E extends Comparable<?superE>> Optional<E> min(List<E>list) {
     int result = reduce(list, 0, (a, l) -> a.compareTo((int)l) <= 0 ? a:(int)l);
     return (Optional<E>) Optional.of(result);
}

I really don't know how to solve this.

1 Answers

It would be so much easier written as:

return list.stream().min(Comparator.comparing(Function.identity()));
Related