Java中数组或集合声明使用的一个小技巧
2016-10-28 10:31
375 查看
平常看项目源代码,我们可以发现,很多集合都有如下类似的写法
关于这个引用 Effective Java 一书中有明确的提到,如下:
Effective Java,Item #43 ” Return empty arrays or collections , not null” demonstrates returning an empty collection and perhaps even demonstrates using these emptyList() , emptySet() , and emptyMap() methods on the Collections class to get an empty collection that also has the additional benefit of being immutable.From Effective Java,Item #15 “Minimize Mutability”.
From Collections-emptySet-Collections-emptyList-Collections-emptyMap
Its a type of programming idiom. This is for people that do not want null variables. So before the set get initialized, they can use the empty set.
**Note:**Below clode is just example(change it according to your use case)
These methods offer o couple of advantages:
They`re more concise because you don’t need to explicitly type out the generic type of the collection - it’s generally just inferred from the context of the method call.
They’re more efficient because they don’t bother creating new objects; they just re-use an existing empty and immutable object. This effect is generally very minor,but it’s occasionally(well,rarely) important.
以上内容摘录自 Effective Java 第43条
Collections.emptyList(),Collections.emptySet(),Collections.emptyMap(),返回的是一个空数组或者一个空集合而不是空对象,同时这三个方法声明的集合符合了Effective Java 15条 减少不可变,代码快的initSet(),initSet2(),initSet3(),就证明了可以在使用的时候动态定义自己集合或者数组所要存放的数据类型.同时代码快的mySet变量的声明保证了不为空,并引用已经存在的空的数组或者集合且不可变的对象,这个也可以防止代码中set集合没有初始化为空的可能。对于以上数组或者集合的声明方法,effective java中是这么说的这种影响通常是很小的,但它的偶尔(以及,很少)重要。
List<String> mList = Collections.EMPTY_LIST; Set<String> mSet = Collections.EMPTY_SET; Map<String> mMap = Collections.EMPTY_MAP;
关于这个引用 Effective Java 一书中有明确的提到,如下:
Effective Java,Item #43 ” Return empty arrays or collections , not null” demonstrates returning an empty collection and perhaps even demonstrates using these emptyList() , emptySet() , and emptyMap() methods on the Collections class to get an empty collection that also has the additional benefit of being immutable.From Effective Java,Item #15 “Minimize Mutability”.
From Collections-emptySet-Collections-emptyList-Collections-emptyMap
Its a type of programming idiom. This is for people that do not want null variables. So before the set get initialized, they can use the empty set.
**Note:**Below clode is just example(change it according to your use case)
private Set myset = Collections.emptySet(); void putData() { mySet.add("test") } void initSet() { myset = new HashSet(); } void initSet2() { myset = new HashSet<String>(); } void initSet3() { myset = new HashSet<Integer>(); } void deleteSet() { myset = Collections.emptySet(); }
These methods offer o couple of advantages:
They`re more concise because you don’t need to explicitly type out the generic type of the collection - it’s generally just inferred from the context of the method call.
They’re more efficient because they don’t bother creating new objects; they just re-use an existing empty and immutable object. This effect is generally very minor,but it’s occasionally(well,rarely) important.
以上内容摘录自 Effective Java 第43条
Collections.emptyList(),Collections.emptySet(),Collections.emptyMap(),返回的是一个空数组或者一个空集合而不是空对象,同时这三个方法声明的集合符合了Effective Java 15条 减少不可变,代码快的initSet(),initSet2(),initSet3(),就证明了可以在使用的时候动态定义自己集合或者数组所要存放的数据类型.同时代码快的mySet变量的声明保证了不为空,并引用已经存在的空的数组或者集合且不可变的对象,这个也可以防止代码中set集合没有初始化为空的可能。对于以上数组或者集合的声明方法,effective java中是这么说的这种影响通常是很小的,但它的偶尔(以及,很少)重要。
相关文章推荐
- 在java中怎么样从一个集合中截取一定长度的元素放到一个新集合中,不是数组哦!。
- 关于Hibernate中fatch=eager的bag集合(一个java List)使用Criteria查询出现重复记录的问题
- android中使用jni 在c++层返回一个数组到java
- 使用java编写的一个遍历未知维数的数组函数
- java中数组的声明及其使用
- 如何将一个java对象(包括java对象数组、集合)转换成一个json字符串
- Java基础——数组的声明,初始化与使用
- Java-数组的声明与使用
- [java] 使用一个类创建数组
- (旋转数组问题)给定一个整数类型的循环有序数组,求循环数组的特定值,使用二分查找法(JAVA实现)
- IOS小技巧——使用FMDB时如何把一个对像中的NSArray数组属性存到表中
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- java数组、集合使用及相互转换
- leetcode-java.T015_3Sum---给定一个n个元素的数组,是否存在a,b,c三个元素,使用得a+b+c=0,找出所有符合这个条件的三元组
- JS数组一个优化小技巧和对象方法的使用
- 【Java】提取JSON数值时遇到数组集合时使用的K-V方式转换
- Java:计算回文数,输入一个五位正整数,使用数组判断它是不是回文数
- java 编写程序实现从控制台接收一个 5 位以上的整数,使用数组来判断该数字 * 是否是回文数。(例如:789987,12344321是回文数)
- 安卓使用Gson解析服务器返回Json数组,以及java服务器怎样返回list集合的Json数据(原创)
- java不使用递归 将一个有层级结构的集合还原成一棵树