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的更多相关文章

  1. java工具类之Arrays、Collections以及比较器

    一.Comparable和Comparator的详解 Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实 ...

  2. Effective java 43返回零长度的数组或者集合而不是null

  3. 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 ...

  4. 《Effective Java》读书笔记 - 7.方法

    Chapter 7 Methods Item 38: Check parameters for validity 直接举例吧: /** * ...其他的被我省略了 * @throws Arithmet ...

  5. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)

    一:工具类 –不存储数据,而是在数据容器上,实现高效操作 • 排序 • 搜索 –Arrays类 –Collection类 二:Arrays类(处理数组) (一)基本方法 –排序:对数组排序, sort ...

  8. Effective Java 第三版——43.方法引用优于lambda表达式

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

随机推荐

  1. [git]安装git-pylint-commit-hook提高python项目中的代码质量

    什么是'git-pylint-commit-hook' 我在工作中,团队为了保证代码和提高代码的质量,要求每个项目都要求安装git-pylint-commit-hook,它是个钩子,会在你提交代码到本 ...

  2. ASP.NET 运行时详解 揭开请求过程神秘面纱

    对于ASP.NET开发,排在前五的话题离不开请求生命周期.像什么Cache.身份认证.Role管理.Routing映射,微软到底在请求过程中干了哪些隐秘的事,现在是时候揭晓了.抛开乌云见晴天,接下来就 ...

  3. SQL年月日方面的查询信息

    这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DAT ...

  4. 2015 Multi-University Training Contest 1 - 1002 Assignment

    Assignment Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Mean: 给你一个数列和一个k,求连续区间的极值之 ...

  5. Web开发者选择的最佳HTML5/CSS3代码生成器

    原文地址:http://codecloud.net/css3-code-generators-for-web-programmers-6672.htmlHTML5 和CSS3是一入门就能用的最好的语言 ...

  6. C#中Guid类型值如何判断不是初始值!

    示例: public Guid _CurrentApplayInfoID { get; set; } 如何判断不是初始值"00000000-0000-0000-0000-0000000000 ...

  7. 与众不同 windows phone (45) - 8.0 语音: TTS, 语音识别, 语音命令

    [源码下载] 与众不同 windows phone (45) - 8.0 语音: TTS, 语音识别, 语音命令 作者:webabcd 介绍与众不同 windows phone 8.0 之 语音 TT ...

  8. OAuth2.0 基础概述

    web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...

  9. 小型app开发的思路

    前提: 1. 性能不是最重要: 2. 人手少: 3. 速度要快: 结论: 1. 混合式 2. 减少app的复杂程度 3. 追求性能 (博客,尽量让自己每天写一点,短一点都可以)

  10. u-boot移植总结(二)LED点灯调试 和 u-boot加载地址

    (一)LED点灯调试 FL2440电路总共有4个LED0,LED1,LED2,LED3,分别接到板子GPB5,GPB6,GPB8,GPB10引脚.通过设置三个寄存器GPBCON(0x56000010) ...