基于Java 11,结源码理解,整理常用类库方法知识点
Objects
java.util.Objects类,为Object工具类,提供大量静态方法进行对象操作和初始状态检查。结合源码整理常用方法如下:
equals
如果参数相等返回 true
,否则返回 false
源码:
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
源码解读:
Objects.equals(Object a,Object b),第一个参数a为null,返回false;当两个参数a、b都为null时返回true;当第一个参数a不为null时,调用a.equals()方法并返回结果。注意重写对象a的类的equals方法。
例:
public class Demo1 {
public static void main(String[] args) {
Person person1 = new Person("张三");
Person person2 = new Person("李四");
Person person3 = new Person("张三");
Person person4 = null;
System.out.println(Objects.equals(person1, person2)); //结果为false
System.out.println(Objects.equals(person1, person3)); //结果为true
System.out.println(Objects.equals(person4, person1)); //结果为false
}
}
class Person {
String name;
public Person(String name) {
this.name = name;
}
public Person() {
}
//重写Person类的equals方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name);
}
}
deepEquals
源码:
public static boolean deepEquals(Object a, Object b) {
if (a == b)
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);
}
源码解读:
Objects.deepEquals(Object a,object b),如果a==b返回true;如果a为null,返回false;如果a为null,b为null,返回true;调用Arrays.deepEquals0(a,b)进行比较,如果a和b都不是数组,则调用a.equals(b),否则进行深层次的比较。
Arrays.deepEquals0()源码:
static boolean deepEquals0(Object e1, Object e2) {
assert e1 != null;
boolean eq;
if (e1 instanceof Object[] && e2 instanceof Object[])
eq = deepEquals ((Object[]) e1, (Object[]) e2);
else if (e1 instanceof byte[] && e2 instanceof byte[])
eq = equals((byte[]) e1, (byte[]) e2);
else if (e1 instanceof short[] && e2 instanceof short[])
eq = equals((short[]) e1, (short[]) e2);
else if (e1 instanceof int[] && e2 instanceof int[])
eq = equals((int[]) e1, (int[]) e2);
else if (e1 instanceof long[] && e2 instanceof long[])
eq = equals((long[]) e1, (long[]) e2);
else if (e1 instanceof char[] && e2 instanceof char[])
eq = equals((char[]) e1, (char[]) e2);
else if (e1 instanceof float[] && e2 instanceof float[])
eq = equals((float[]) e1, (float[]) e2);
else if (e1 instanceof double[] && e2 instanceof double[])
eq = equals((double[]) e1, (double[]) e2);
else if (e1 instanceof boolean[] && e2 instanceof boolean[])
eq = equals((boolean[]) e1, (boolean[]) e2);
else
eq = e1.equals(e2);
return eq;
}
如果比较的两个对象e1,e2都不是数组对象,则调用e1.equals(e2)返回结果。
如果是基本数据类型的数组,则调用Arrays的equals。如果不是基本类型数据则调用Arrays.deepEquals方法。
------------------------------------------------------------------------
Arrays.equals源码
public static boolean equals(short[] a, short a2[]) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
return ArraysSupport.mismatch(a, a2, length) < 0;
}
------------------------------------------------------------------------
Arrays.deepEquals源码:
public static boolean deepEquals(Object[] a1, Object[] a2) {
if (a1 == a2)
return true;
if (a1 == null || a2==null)
return false;
int length = a1.length;
if (a2.length != length)
return false;
for (int i = 0; i < length; i++) {
Object e1 = a1[i];
Object e2 = a2[i];
if (e1 == e2)
continue;
if (e1 == null)
return false;
// Figure out whether the two elements are equal
boolean eq = deepEquals0(e1, e2);
if (!eq)
return false;
}
return true;
}
hashCode
源码:
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
源码解读:
如果参数o为null,返回0;不为null,调用该对象的hashCode方法返回hashCode。
toString
源码:
public static String toString(Object o) {
return String.valueOf(o);
}
源码解读:
调用String.valueOf(Object o)
------------------------------------------------------------------------
String.valusOf 源码:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
如果参数为null,则返回"null",否则返回调用该参数的toString()方法结果。
源码:
public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}
源码解读:
如果参数为null,则返传入的默认的字符串 String nullDefault;否则返回调用参数的toString方法结果。
compare
如果参数相同则返回0,否则返回 c.compare(a, b)
。
源码:
public static <T> int compare(T a, T b, Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
源码解读:
如果a==b,则返回0,否则根据自定义的比较期Comparator c进行比较
requireNonNull
源码:
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
源码解读:
如果参数为null,抛出异常;否则返回该参数对象。该方法用于检查对象不能为null。
isNull
源码:
public static boolean isNull(Object obj) {
return obj == null;
}
源码解读:
判断参数对象是否为空,是返回true,不是返回false;
nonNull
源码:
public static boolean nonNull(Object obj) {
return obj != null;
}
源码解读:
判断参数对象是否为空,不是返回true,是空返回false;
requireNonNullElse
源码:
public static <T> T requireNonNullElse(T obj, T defaultObj) {
return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
}
源码解读:
参数不为null,返回该参数;为空,返回指定的默认对象。
checkIndex
检查 index
是否在 0
(含)到 length
(不包括)范围内
源码:
public static int checkIndex(int index, int length) {
return Preconditions.checkIndex(index, length, null);
}
源码解读:
调用Preconditions.checkIndex(index, length, null)进行下标检查
-----------------------------------------------------------------------------------------
Preconditions.checkIndex源码:
public static <X extends RuntimeException>
int checkIndex(int index, int length,
BiFunction<String, List<Integer>, X> oobef) {
if (index < 0 || index >= length)
throw outOfBoundsCheckIndex(oobef, index, length);
return index;
}
源码解读:
如果index<0或者index>=length,抛出outOfBoundsCheckIndex异常;否则返回index
checkFromToIndex
检查是否在子范围从 fromIndex
(包括)到 toIndex
(不包括)是范围界限内 0
(包括)到 length
(不包括)。
源码:
public static
int checkFromToIndex(int fromIndex, int toIndex, int length) {
return Preconditions.checkFromToIndex(fromIndex, toIndex, length, null);
}
源码解读:
调用Preconditions.checkFromToIndex(fromIndex, toIndex, length, null)进行下标检查
-----------------------------------------------------------------------------------------
Preconditions.checkFromToIndex源码:
public static <X extends RuntimeException>
int checkFromToIndex(int fromIndex, int toIndex, int length,
BiFunction<String, List<Integer>, X> oobef) {
if (fromIndex < 0 || fromIndex > toIndex || toIndex > length)
throw outOfBoundsCheckFromToIndex(oobef, fromIndex, toIndex, length);
return fromIndex;
}
源码解读:
如果起始下标fromIndex < 0或者起始下标fromIndex > 终点下标toIndex或者起始下标大于长度toIndex > length,抛出outOfBoundsCheckFromToIndex异常;否则返回起始下标fromIndex
checkFromIndexSize
检查是否在子范围从 fromIndex
(包括)到 fromIndex + size
(不包括)是范围界限内 0
(包括)到 length
(不包括)。
源码:
public static
int checkFromIndexSize(int fromIndex, int size, int length) {
return Preconditions.checkFromIndexSize(fromIndex, size, length, null);
}
源码解读:
调用Preconditions.checkFromIndexSize(fromIndex, size, length, null)返回结果。
----------------------------------------------------------------------------------------
Preconditions.checkFromIndexSize源码:
public static <X extends RuntimeException>
int checkFromIndexSize(int fromIndex, int size, int length,
BiFunction<String, List<Integer>, X> oobef) {
if ((length | fromIndex | size) < 0 || size > length - fromIndex)
throw outOfBoundsCheckFromIndexSize(oobef, fromIndex, size, length);
return fromIndex;
}
源码解读:
如果长度length或起始下标fromIndex或大小size小于0,或者大小大于长度length-fromIdex,抛出 outOfBoundsCheckFromIndexSize异常;都在返回起始下标fromIndex