1.目的
避免因为返回null,导致调用端代码必须检查返回值是否为null
2.方法
2.1返回长度为0的数组
private static final T[] EMPTY_ARRAY = new T[0];
public T[] getArray() {
return EMPTY_ARRAY;
}
2.2返回空集合
Collection.emptyList();
Collection.emptySet();
Collection.emptyMap();
避免因为返回null,导致调用端代码必须检查返回值是否为null
private static final T[] EMPTY_ARRAY = new T[0];
public T[] getArray() {
return EMPTY_ARRAY;
}
Collection.emptyList();
Collection.emptySet();
Collection.emptyMap();