简体   繁体   English

如何在列表中找到最小值<e> ?</e>

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

I tried it this way but all I get is this Error Message: java: incompatible types: E cannot be converted to int我尝试过这种方式,但我得到的只是这个错误消息: java:不兼容的类型:E 无法转换为 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.我真的不知道如何解决这个问题。

It would be so much easier written as:写成这样会容易得多:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM