0

Например:

public double getMax() {
    try {
        if (list != null && !list.isEmpty()) {
            double max = list.get(0).x + list.get(0).y;
            for (Object obj : list) {
                if (obj.x + obj.y > max)
                    max = obj.x + obj.y;
            }
            return max;
        } else {
            throw new NoSuchElementException();
        }
    } catch (NoSuchElementException e){
        return 0;
    }
}
4
  • 1
    а где тут лямбда-выражение?
    – IR42
    3 июн 2020 в 17:14
  • при помощи лямбда выражения описать try catch
    – hellog888
    3 июн 2020 в 18:22
  • @hellog888 никак
    – NarasuOo
    3 июн 2020 в 22:05
  • orElseThrow?
    – vp_arth
    5 июн 2020 в 10:03

1 ответ 1

1
double max;
if (list == null) {
    max = 0.0
} else {
    max = list.stream()
        .mapToDouble(it -> it.x + it.y)
        .max()
        .orElse(0.0);
}
1
  • пожалуйста, постарайтесь оставлять чуть более развёрнутые ответы. дополнить ответ можно, нажав править 9 июн 2020 в 13:06

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.