Информация об изменениях

Сообщение Легко ли читается method reference вместо lambda? от 14.09.2022 22:23

Изменено 14.09.2022 22:23 vsb

Легко ли читается method reference вместо lambda?
Меня очень раздражает синтаксис method reference (где :). Я решительно не понимаю, зачем он нужен и на мой взгляд он ухудшает читаемость кода. Хотелось бы услышать стороннее мнение. Приведу несколько фрагментов кода.

    appointmentRepository.queryByIds(appointmentIds).stream()
        .filter(Objects::nonNull)

    appointmentRepository.queryByIds(appointmentIds).stream()
        .filter(appointment -> nonNull(appointment))

    appointmentRepository.queryByIds(appointmentIds).stream()
        .filter(appointment -> appointment != null)
`

    Set<String> acceptedIins = response.stream()
        .map(SomeApiClient.AddReferrersResponseItem::getIin)
        .collect(toSet());

    Set<String> acceptedIins = response.stream()
        .map(responseItem -> responseItem.getIin())
        .collect(toSet());


Я предполагаю, что method reference может компилироваться в незначительно более быстрый код, но уверен, что разница неизмерима на практике.
Легко ли читается method reference вместо lambda?
Меня очень раздражает синтаксис method reference (где ::). Я решительно не понимаю, зачем он нужен и на мой взгляд он ухудшает читаемость кода. Хотелось бы услышать стороннее мнение. Приведу несколько фрагментов кода.

    appointmentRepository.queryByIds(appointmentIds).stream()
        .filter(Objects::nonNull)

    appointmentRepository.queryByIds(appointmentIds).stream()
        .filter(appointment -> nonNull(appointment))

    appointmentRepository.queryByIds(appointmentIds).stream()
        .filter(appointment -> appointment != null)
`

    Set<String> acceptedIins = response.stream()
        .map(SomeApiClient.AddReferrersResponseItem::getIin)
        .collect(toSet());

    Set<String> acceptedIins = response.stream()
        .map(responseItem -> responseItem.getIin())
        .collect(toSet());


Я предполагаю, что method reference может компилироваться в незначительно более быстрый код, но уверен, что разница неизмерима на практике.