java Apache Commons 包中的 IntegerUtils 和 DoubleUtils
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15692999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntegerUtils and DoubleUtils in Apache Commons package
提问by Venk K
I use the Apache Commons package extensively, especially the StringUtils, BooleanUtils, ObjectUtils, MapUtils classes and find them extremely helpful. I am wondering if there are classes such as IntegerUtils, DoubleUtils that provide a similar functionality for their respective wrapper classes (I do not find such classes in the Apache Commons package).
我广泛使用 Apache Commons 包,尤其是 StringUtils、BooleanUtils、ObjectUtils、MapUtils 类,并发现它们非常有用。我想知道是否有诸如 IntegerUtils、DoubleUtils 之类的类为其各自的包装类提供了类似的功能(我在 Apache Commons 包中没有找到此类类)。
Thanks,
谢谢,
Venkat
文卡特
回答by Kip
I wish they had a utilities class for numbers as useful as the one for strings. NumberUtils class is all about converting numbers to/from strings.
我希望他们有一个用于数字的实用程序类,与用于字符串的实用程序类一样有用。NumberUtils 类是关于将数字转换为/从字符串转换的。
You can use ObjectUtilsto do null-safe Integer operations though.
不过,您可以使用ObjectUtils来执行空安全的整数操作。
Instead of:
代替:
foo(Integer arg) {
if(arg != null && arg == 1)
doSomething();
}
You can do:
你可以做:
foo(Integer arg) {
if(ObjectUtils.defaultIfNull(arg, 0) == 1)
doSomething();
}
In the case where the Integer
you are comparing is, say, a function call that returns an Integer
, this will allow you to only call the function once without creating a throwaway variable.
如果Integer
您正在比较的是一个返回 an 的函数调用Integer
,这将允许您只调用该函数一次而不会创建一次性变量。