Optional vs if/else-if performance java 8

前端 未结 5 1641
不思量自难忘°
不思量自难忘° 2021-02-19 23:08

Hello i have two samples of code

if/else if/else statements

private Object getObj(message) {
        if (message         


        
5条回答
  •  误落风尘
    2021-02-19 23:37

    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.

提交回复
热议问题