Objects类
类包含static实用程序方法,用于操作对象或在操作前检查某些条件。 这些实用程序包括null或null方法,用于计算对象的哈希代码,返回对象的字符串,比较两个对象,以及检查索引或子范围值是否超出范围。
public static int checkFromIndexSize(int fromIndex, int size, int length)
- 功能:检查子范围[fromIndex, fromIndex + size)是否在范围界限[0,length)内。
- 抛出IndexOutOfBoundsException异常:
- fromIndex < 0
- size < 0
- fromIndex + size > length ,考虑到整数溢出
- length < 0 (同上)
int[] array = {3,4,6,2,6};
int result1 = Objects.checkFromIndexSize(1,3,array.length);
System.out.println(result1);
//1
int result2 = Objects.checkFromIndexSize(2,4,array.length);
System.out.println(result2);
//Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [2, 2 + 4) out of bounds for length 5
public static int checkFromToIndex(int fromIndex, int toIndex, int length)
- 功能:检查子范围[fromIndex,toIndex)是否在范围界限[0,length)内。
- 抛出IndexOutOfBoundsException异常:
- fromIndex < 0
- fromIndex > toIndex
- toIndex > length
- length < 0 (同上)
int[] array = {3,4,6,2,6};
int result1 = Objects.checkFromToIndex(1,4,array.length);
System.out.println(result1);
//1
int result2 = Objects.checkFromToIndex(2,6,array.length);
System.out.println(result2);
//Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [2, 6) out of bounds for length 5
public static int checkIndex(int index, int length)
- 功能:检查index是否在[0,length)范围内。
- 抛出IndexOutOfBoundsException异常:
- index < 0
- index >= length
- length < 0 (同上)
int[] array = {3,4,6,2,6};
int result1 = Objects.checkIndex(4,array.length);
System.out.println(result1);
//1
int result2 = Objects.checkIndex(6,array.length);
System.out.println(result2);
//Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 6 out of bounds for length 5
public static <T> int compare(T a, T b, Comparator<? super T> c)
- 功能:如果参数相同则返回0,否则返回c.compare(a, b) 。 因此,如果两个参数都是null则返回0。
Person p1 = new Person("zhangsan",18);
Person p2 = new Person("lisi", 4);
System.out.println(Objects.compare(p1, p2, new Comparator<Person>(){
public int compare(Person o1, Person o2) {
return o1.getAge().compareTo(o2.getAge());
}
}));
//18, 4 -> 1
//18, 18 -> 0
//18, 34 -> -1
public static boolean deepEquals(Object a, Object b)
- 功能:如果参数之间deeply equal则返回true,否则返回false。 两个null值deeply equal,返回true(a==b)。 一个参数为null返回false。如果两个参数都是数组,则使用Arrays.deepEquals中的算法来确定相等性。 否则,通过使用第一个参数的equals方法确定相等性。deep.Equals算法作用于8种数据类型/Object元素构成的数组,对每个元素进行
==
比较。
int[] array1 = {1,2,3,4};
int[] array2 = {1,2,3,4};
int[] array3 = {1,2,3,4,5,6};
System.out.println(Objects.deepEquals(array1, array2));
//true
System.out.println(Objects.deepEquals(array1, array3));
//false
public static boolean equals(Object a, Object b)
- 功能:如果参数相等返回true,否则false。 因此,如果这两个参数是null,返回true,如果只有一个参数为null,false返回。 否则,通过使用第一个参数的equals方法确定相等性。equals方法为if(a==b)(比较栈内存地址),可以重写。
- 源码
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
public boolean equals(Object obj) {
return (this == obj);
}
int[] array1 = {1,2,3,4};
int[] array2 = {1,2,3,4};
int[] array3 = {1,2,3,4,5,6};
System.out.println(Objects.equals(array1, array2));
//false
System.out.println(Objects.equals(array1, array3));
//false
int[] array4 = array1;
System.out.println(Objects.equals(array1,array4));
//true
- className.equals()方法存在空指针问题,需要加入判空语句
- Objects.equals()可以解决空指针问题
String s1 = null;
String s2 = "456";
System.out.println(s1.equals(s2));
//出现空指针异常
Person p1 = null;
Person p2 = new Person();
System.out.println(p1.equals(p2));
//出现空指针异常
System.out.println(Objects.equals(p1, p2));
//false
Person p1 = null;
Person p2 = null;
System.out.println(Objects.equals(p1, p2));
//true
public static int hash(Object... values)
- 功能:为一系列输入值生成哈希码。 生成哈希码,好像所有输入值都放在一个数组中,并通过调用Arrays.hashCode(Object[])对该数组进行哈希处理 。
int array4 = Objects.hash(1, 2, 3);
System.out.println(array4);
//30817
public static int hashCode(Object o)
- 功能:若参数对象为空,返回整数0;若不为空,返回整型数值,表示该对象的哈希码值
int b = 100;
System.out.println(Objects.hashCode(b));
//100
public static boolean isNull(Object obj)
- 功能:参数为null,返回true
String s = null;
System.out.println(Objects.isNull(s));
//true
public static boolean nonNull(Object obj)
- 功能:参数为null,返回false
String s = null;
System.out.println(Objects.nonNull(s));
//false
public static <T> T requireNonNull(T obj)
- 功能:检查指定的对象引用是否不是null。此方法主要用于在方法和构造函数中进行参数验证。
String s1 = "hahah";
String s2 = null;
System.out.println(Objects.requireNonNull(s1));
//hahah
System.out.println(Objects.requireNonNull(s2));
//Exception in thread "main" java.lang.NullPointerException
- 两种其他格式:
requireNonNull(T obj, String message)
requireNonNull(T obj, Supplier<String> messageSupplier)
System.out.println(Objects.requireNonNull(s2, "输入为空"));
//Exception in thread "main" java.lang.NullPointerException: 输入为空
Objects.requireNonNull(s2, new Supplier<String>(){
@Override
public String get() {
return "The Object cann't be null...";
});
public static <T> T requireNonNullElse(T obj, T defaultObj)
- 功能:如果它是非null,则返回第一个参数,否则返回非null第二个参数。
String s1 = "hahah";
String s2 = null;
System.out.println( Objects.requireNonNullElse(s2,s1));
public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier)
- 功能:如果它是非null,则返回第一个参数,否则调用get()返回非null值。
System.out.println(Objects.requireNonNullElseGet(s2,new Supplier<String>() {
@Override
public String get() {
return "The Object cann't be null...";
}
}));
public static String toString(Object o)
- 功能:返回对一个非空参数调用toString的结果,对一个空参数返回 “null”。
String s1 = null;
System.out.println(Objects.toString(s1));
//"null"
boolean s2 = true;
System.out.println(Objects.toString(s2));
//"true"
int s3 = 123;
System.out.println(Objects.toString(s3));
//"123"
public static String toString(Object o, String nullDefault)
- 功能:如果第一个参数不是 null,则返回在第一个参数上调用toString的结果,否则返回第二个参数
String s1 = null;
String s2 = "123";
System.out.println(Objects.toString(s1, s2));
//"123"