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. android程序---->android多线程下载(一)

    多线程下载是加快下载速度的一种方式,通过开启多个线程去执行一个任务,可以使任务的执行速度变快.多线程的任务下载时常都会使用得到断点续传下载,就是我们在一次下载未结束时退出下载,第二次下载时会接着第一次 ...

  2. python内置模块(3)

    博主所有python文章均为python3.5.1环境. 目录: 1.collections 2.subprocess 3.configparser 4.XML 5.shutil 一. collect ...

  3. eclipse中 properties文件编码问题

    1. Eclipse修改设置 项目中用到了配置文件,所以在Eclipse中新建.properties文件,文件中编辑了中文,在保存时Eclipse报出以下错误: 解决这个问题的方法: 依次选择: 菜单 ...

  4. ASP.NET MVC5--为数据库新增字段(涉及数据库迁移技术)

    Setting up Code First Migrations for Model Changes--为模型更改做数据库迁移. 1.打开资源管理器,在App_Data文件夹下,找到movies.md ...

  5. js跳转页面方法大全

    js跳转页面方法大全<span id="tiao">3</span><a href="javascript:countDown"& ...

  6. .Net Oauth2.0 第三方登录开发(Facebook ,LinkedIn )

    需求:OAuth2实现第三方网站授权并获取其相关数据来实现登录等功能 暂时支持Facebook ,LinkedIn ,基本大同小异,只是返回时的数据不同,需根据具体返回类型进行相应处理 1.OAuth ...

  7. 根据数据库输出XML菜单

    USE [test_YTHH] GO /****** Object:  StoredProcedure [dbo].[usp_Print_SCC_Menu]    Script Date: 04/08 ...

  8. iis到w3wp的数据流及工作原理

    HTTP.sys->IO线程-CLR线程池中的worker线程处理 IO线程只负责把请求交给Worker线程或者放入进程池级别的队列,然后又去HTTP.SYS的队列中处理其它的请求

  9. Programming in Go (Golang) – Setting up a Mac OS X Development Environment

    http://www.distilnetworks.com/setup-go-golang-ide-for-mac-os-x/#.V1Byrf50yM8 Programming in Go (Gola ...

  10. jvisualvm远程监控jvm设置

    有些时候,需要对特定环境中的Java应用进行实时性能分析,大部分非开发和测试环境,一般都是用jvisualvm进行基本检测以最小化对系统的影响(其开启后,负载影响大约20%-30%),jvisualv ...