Effective java学习笔记:第43条 返回0长度的数组或者集合,而不是null

来源:互联网 发布:淘宝卖家订单管理系统 编辑:程序博客网 时间:2024/05/29 19:33

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();
0 0