Is there a shortcut to execute something only if its not null?

This question already has an answer here:

  • Avoiding != null statements 58 answers

  • In Java 8:

    static <T> boolean notNull(Supplier<T> getter, Predicate<T> tester) {
        T x = getter.get();
        return x != null && tester.test(x);
    }
    
        if (notNull(something::getThatObject, MyObject::someBooleanFunction)) {
            ...
        }
    

    If this style is new to the readers, one should keep in mind, that full functional programming is a bit nicer.


    Well Java 8 has got something called Optional. More details are at : http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html


    不,我相当确信,没有办法做到比你所拥有的任何时间都短。

    链接地址: http://www.djcxy.com/p/13296.html

    上一篇: Java:空检查的最佳实践

    下一篇: 只有当它不为空时才有执行某个快捷方式的快捷方式?