我广泛使用 Apache Commons 包,尤其是 StringUtils、BooleanUtils、ObjectUtils、MapUtils 类,并发现它们非常有用。我想知道是否有 IntegerUtils、DoubleUtils 等类为其各自的包装类提供类似的功能(我在 Apache Commons 包中没有找到此类类)。
谢谢
文卡特
最佳答案
我希望他们有一个用于数字的实用程序类,就像用于字符串的实用程序类一样有用。 NumberUtils 类主要用于将数字与字符串相互转换。
您可以使用ObjectUtils进行空安全整数运算。
而不是:
foo(Integer arg) {
if(arg != null && arg == 1)
doSomething();
}
你可以这样做:
foo(Integer arg) {
if(ObjectUtils.defaultIfNull(arg, 0) == 1)
doSomething();
}
如果您要比较的Integer
是一个返回Integer
的函数调用,这将允许您只调用该函数一次而无需创建一次性变量。
关于java - Apache Commons 包中的 IntegerUtils 和 DoubleUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15692999/