在Java 8中添加了Supplier的方法Objects.requireNonNull,但是我不确定声明的性能改进是什么:
While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.
带String的方法如果不为null则忽略参数:
public static T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
In JDK 7, java.util.Objects included several methods to check for null, including one that took a message to return if a null was found. With lambdas in JDK 8, another variant to include is a requireNonNull method which takes a string supplier instead of a string. That why the cost of creating the string message can be avoided for the non-null case. Note that the lambda capture can have a nonzero cost though.
有注释表示对性能没有影响:
The non-zero capture cost does worry me. I am concerned that it will frequently erase any advantage of using a Supplier.
09-04-2013
我发现了其他问题,但没有提到(为什么)发送String参数会降低性能
它专用于lambda表达式/流用法吗?