Java标准库中是否存在具有静态相等函数的任何地方?
public static <T> boolean equals(T a,T b) { if (a == null) return b == null; else if (b == null) return false; else return a.equals(b); }
我刚刚在一个新的项目Util类中实现了这个,无数次.似乎令人难以置信的是它不会作为标准库函数发布……
解决方法
在JDK 7中有
Objects#equals().来自Javadoc:
Returns true if the arguments are equal to each other and false
otherwise. Consequently,if both arguments are null,true is returned
and if exactly one argument is null,false is returned. Otherwise,
equality is determined by using the equals method of the first
argument.
除了Apache Commons Lang中已经提到的功能之外,Google Guava中还有一个功能,Objects#equal():