public final class Objects
extends Object
| java.lang.Object | |
| java.util.Objects | |
该类由static用于对对象进行操作的实用程序方法组成。 这些实用程序包括null或null容错方法,用于计算对象的哈希码,返回对象的字符串以及比较两个对象。
公共方法(Public methods) |
|
|---|---|
static <T> int |
compare(T a, T b, Comparator<? super T> c) 如果参数相同则返回0;否则返回 |
static boolean |
deepEquals(Object a, Object b) 返回 |
static boolean |
equals(Object a, Object b) 返回 |
static int |
hash(Object... values) 为一系列输入值生成哈希码。 |
static int |
hashCode(Object o) 返回非 |
static boolean |
isNull(Object obj) 返回 |
static boolean |
nonNull(Object obj) 返回 |
static <T> T |
requireNonNull(T obj, String message) 检查指定的对象引用是否为 |
static <T> T |
requireNonNull(T obj, Supplier<String> messageSupplier) 检查指定的对象引用不是 |
static <T> T |
requireNonNull(T obj) 检查指定的对象引用不是 |
static String |
toString(Object o) 返回调用的结果 |
static String |
toString(Object o, String nullDefault) 如果第一个参数不是 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
int compare (T a,
T b,
Comparator<? super T> c)
如果参数相同则返回0,否则返回c.compare(a, b) 。 因此,如果两个参数都是null则返回0。
请注意,如果其中一个参数是 null ,则 NullPointerException可能会或可能不会被抛出,具体取决于订购策略(如果有), Comparator选择 null值为 null 。
| 参数(Parameters) | |
|---|---|
a |
T: an object |
b |
T: an object to be compared with a |
c |
Comparator: the Comparator to compare the first two arguments |
| 返回(Returns) | |
|---|---|
int |
0 if the arguments are identical and c.compare(a, b) otherwise. |
也可以看看:
boolean deepEquals (Object a, Object b)
返回true如果参数是深层相等,彼此false否则。 两个null值是非常相等的。 如果两个参数都是数组,则使用Arrays.deepEquals的算法确定相等性。 否则,通过使用第一个参数的equals方法确定相等性。
| 参数(Parameters) | |
|---|---|
a |
Object: an object |
b |
Object: an object to be compared with a for deep equality |
| 返回(Returns) | |
|---|---|
boolean |
true if the arguments are deeply equal to each other and false otherwise |
boolean equals (Object a, Object b)
返回true如果参数相等,彼此false否则。 因此,如果这两个参数是null , true返回,如果只有一个参数为null , false返回。 否则,通过使用第一个参数的equals方法确定相等性。
| 参数(Parameters) | |
|---|---|
a |
Object: an object |
b |
Object: an object to be compared with a for equality |
| 返回(Returns) | |
|---|---|
boolean |
true if the arguments are equal to each other and false otherwise |
也可以看看:
int hash (Object... values)
为一系列输入值生成哈希码。 生成哈希码就好像所有的输入值都放入数组中,并通过调用hashCode(Object[])来哈希该数组。
此方法对于在包含多个字段的对象上实现hashCode()很有用。 例如,如果一个对象具有三个字段, x y和z ,则可以这样写:
@Override public int hashCode() {
return Objects.hash(x, y, z);
}
Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling
hashCode(Object).
| 参数(Parameters) | |
|---|---|
values |
Object: the values to be hashed |
| 返回(Returns) | |
|---|---|
int |
a hash value of the sequence of input values |
也可以看看:
int hashCode (Object o)
返回非 null参数的散列码,并为 null参数返回0。
| 参数(Parameters) | |
|---|---|
o |
Object: an object |
| 返回(Returns) | |
|---|---|
int |
the hash code of a non-null argument and 0 for a null argument |
也可以看看:
boolean isNull (Object obj)
返回 true如果提供的参考是 null ,否则返回 false 。
Predicate, filter(Objects::isNull)| 参数(Parameters) | |
|---|---|
obj |
Object: a reference to be checked against null |
| 返回(Returns) | |
|---|---|
boolean |
true if the provided reference is null otherwise false |
也可以看看:
boolean nonNull (Object obj)
返回 true如果提供的参考是非 null否则返回 false 。
Predicate, filter(Objects::nonNull)| 参数(Parameters) | |
|---|---|
obj |
Object: a reference to be checked against null |
| 返回(Returns) | |
|---|---|
boolean |
true if the provided reference is non-null otherwise false |
也可以看看:
T requireNonNull (T obj,
String message)
检查指定的对象引用不是null ,如果是,则引发自定义的NullPointerException 。 此方法主要用于在具有多个参数的方法和构造函数中进行参数验证,如下所示:
public Foo(Bar bar, Baz baz) {
this.bar = Objects.requireNonNull(bar, "bar must not be null");
this.baz = Objects.requireNonNull(baz, "baz must not be null");
}
| 参数(Parameters) | |
|---|---|
obj |
T: the object reference to check for nullity |
message |
String: detail message to be used in the event that a NullPointerException is thrown |
| 返回(Returns) | |
|---|---|
T |
obj if not null |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if obj is null |
T requireNonNull (T obj,
Supplier<String> messageSupplier)
检查指定的对象引用不是 null ,如果是,则引发自定义的 NullPointerException 。
与方法requireNonNull(Object, String)不同,此方法允许创建要推迟的消息,直到进行空值检查为止。 虽然这可能会赋予非空情况下的性能优势,但在决定调用此方法时应注意创建消息供应商的成本低于仅直接创建字符串消息的成本。
| 参数(Parameters) | |
|---|---|
obj |
T: the object reference to check for nullity |
messageSupplier |
Supplier: supplier of the detail message to be used in the event that a NullPointerException is thrown |
| 返回(Returns) | |
|---|---|
T |
obj if not null |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if obj is null |
T requireNonNull (T obj)
检查指定的对象引用不是null 。 此方法主要用于在方法和构造函数中进行参数验证,如下所示:
public Foo(Bar bar) {
this.bar = Objects.requireNonNull(bar);
}
| 参数(Parameters) | |
|---|---|
obj |
T: the object reference to check for nullity |
| 返回(Returns) | |
|---|---|
T |
obj if not null |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if obj is null |
String toString (Object o)
返回调用的结果 toString的非 null参数, "null"为 null的说法。
| 参数(Parameters) | |
|---|---|
o |
Object: an object |
| 返回(Returns) | |
|---|---|
String |
the result of calling toString for a non-null argument and "null" for a null argument |
也可以看看:
String toString (Object o, String nullDefault)
如果第一个参数不是 null ,则返回第一个参数调用 toString的结果,否则返回第二个参数。
| 参数(Parameters) | |
|---|---|
o |
Object: an object |
nullDefault |
String: string to return if the first argument is null |
| 返回(Returns) | |
|---|---|
String |
the result of calling toString on the first argument if it is not null and the second argument otherwise. |
也可以看看: