Effective Java 43 Return empty arrays or collections, not nulls
|
Feature |
Return empty arrays or collection |
Return nulls |
|
Avoids the expense of allocating the array |
N |
Y |
|
Return value is immutable which may be shared freely |
Y |
N |
Standard idiom for dumping items from a collection into a typed array
public class Shop {
// The right way to return an array from a collection
private final List<Cheese> cheesesInStock = new ArrayList<Cheese>();
// This prevent allocating an new Array every time which handles the expense issue.
private static final Cheese[] EMPTY_CHEESE_ARRAY = new Cheese[0];
/**
* @return an array containing all of the cheeses in the shop.
*/
public Cheese[] getCheeses() {
return cheesesInStock.toArray(EMPTY_CHEESE_ARRAY);
}
public static void main(String[] args) {
Shop shop = new Shop();
// User don't have to check the null of the returned array since it always returns an empty array
if (Arrays.asList(shop.getCheeses()).contains(Cheese.STILTON))
System.out.println("Jolly good, just the thing.");
}
}
A collection-valued method can be made to return the same immutable empty collection every time it needs to return an empty collection.
/**
* The right way to return a copy of a collection
* @return an array containing all of the cheeses in the shop.
*/
public List<Cheese> getCheeseList() {
if (cheesesInStock.isEmpty())
return Collections.emptyList(); // Always returns same list
else
return new ArrayList<Cheese>(cheesesInStock); // This is defensive copy to avoid the mutable object to be modified by the client.
}
Summary
There is no reason ever to return null from an array or collection-valued method instead of returning an empty array or collection.
Effective Java 43 Return empty arrays or collections, not nulls的更多相关文章
- java工具类之Arrays、Collections以及比较器
一.Comparable和Comparator的详解 Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实 ...
- Effective java 43返回零长度的数组或者集合而不是null
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- 《Effective Java》读书笔记 - 7.方法
Chapter 7 Methods Item 38: Check parameters for validity 直接举例吧: /** * ...其他的被我省略了 * @throws Arithmet ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)
一:工具类 –不存储数据,而是在数据容器上,实现高效操作 • 排序 • 搜索 –Arrays类 –Collection类 二:Arrays类(处理数组) (一)基本方法 –排序:对数组排序, sort ...
- Effective Java 第三版——43.方法引用优于lambda表达式
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java通俗理解(下)
Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...
随机推荐
- SAPI训练文件存储位置
查看注册表HKEY_CURRENT_USER\Software\Microsoft\Speech\RecoProfiles 说明查看http://msdn.microsoft.com/en-us/li ...
- 五、Request
1. Request 由于python函数所有变量都没有显示类型声明,特别是函数的输入参数,输出参数,因此在阅读代码时会造成比较大的困扰,比如大部分处理函数都有request输入参数,不同模 ...
- [ASP.NET]分析MVC5源码,并实现一个ASP.MVC
本节内容不是MVC入门教程,主要讲MVC原理,实现一个和ASP.NET MVC类似基本原理的项目. MVC原理是依赖于ASP.NET管道事件基础之上的.对于这块,可阅读上节内容 [ASP.NET]谈谈 ...
- 【WinRT】国内外 Windows 应用商店应用开发者博客收集
本文格式:博主名 博客链接 本人点评.排名不分先后. 中文: 博客园: webabcd http://www.cnblogs.com/webabcd/ 微软最有价值专家(MVP),他做的 Win8.1 ...
- SQL Server密码管理的六个危险判断
当管理SQL Server内在的帐户和密码时,我们很容易认为这一切都相当的安全.但实际上并非如此.在这里,我们列出了一些对于SQL Server密码来说非常危险的判断. 当管理SQL Server内在 ...
- 【poj 3461】Oulipo(字符串--KMP)
题意:求子串在文本串中出现了多少次. 解法:使用KMP的next[ ]和tend[ ]数组计数. #include<cstdio> #include<cstdlib> #inc ...
- 类库LinqToExcel的介绍
LinqToExcel是一个.net framework平台下开源项目,它主要实现了LINQ的语法查询Excel电子表格.类型之前的LINQToXXX如果你是LINQ语法糖爱好者那最适 ...
- Java生成公私钥对
public static synchronized KeyPair generateRSAKeyPair(int keysize, BigInteger publicExponent) { try ...
- [mysql] timestamp自动更新和初始化
1.概述 在我们设计表的时候,考虑将行数据的创建时间和最后更新时间记录下来是很好的实践.尤其是可能需要做数据同步或者对数据新鲜度有要求的表.举些应用场景,更新距上次更新超过2小时的行数据,或者是将一个 ...
- javascript中||和&&代替if
首先,我们来看一段代码: ; ){ add_level = ; } ){ add_level = ; } ){ add_level = ; } ){ add_level = ; } else { ad ...