Optional vs if/else-if performance java 8

前端 未结 5 1752
我寻月下人不归
我寻月下人不归 2021-02-19 22:58

Hello i have two samples of code

if/else if/else statements

private Object getObj(message) {
        if (message         


        
5条回答
  •  别那么骄傲
    2021-02-19 23:40

    There is a third form (allowing still some variation).

    return Stream.>of(message::getA, message::getB, message::getC)
            .map(Supplier::get)
            .filter(Objects::nonNull)
            .findFirst()
            .orElse(null);
    

    Probably the least flexible and efficient at this moment, but clear.

提交回复
热议问题