Effective Java 28 Use bounded wildcards to increase API flexibility
Get and Put Principle
PECS stands for producer-extends(? extends T), consumer-super(? super T).
- For maximum flexibility, use wildcard types on input parameters that represent producers or consumers.
 
// Wildcard type for parameter that serves as an E producer
public void pushAll(Iterable<? extends E> src) {
for (E e : src)
push(e);
}
// Wildcard type for parameter that serves as an E consumer
public void popAll(Collection<? super E> dst) {
while (!isEmpty())
dst.add(pop());
}
Although lists can both consume and produce values, the reduce method uses its list parameter only as an E producer, so its declaration should use a wildcard type that extends E. The parameter f represents a function that both consumes and produces E instances, so a wildcard type would be inappropriate for it. Here's the resulting method declaration:
// Wildcard type for parameter that serves as an E producer
static <E> E reduce(List<? extends E>list, Function<E> f, E initVal)
2. Do not use wildcard types as return types.
public static <E> Set<E> union(Set<? extends E> s1, Set<? extends E> s2)
3. If the user of a class has to think about wildcard types, there is probably something wrong with the class's API.
4. Use explicit parameter for the generic method.
Set<Integer> integers = ... ;
Set<Double> doubles = ... ;
Set<Number> numbers = union(integers, doubles);
Set<Number> numbers = Union.<Number>union(integers, doubles);
Always use Comparable<? super T> in preference to Comparable<T>.The same is true of comparators, so you should always use Comparator<? super T> in preference to Comparator<T>.
public static <T extends Comparable<? super T>> T max(List<? extends T> list)
5. If a type parameter appears only once in a method declaration, replace it with a wildcard for its simplify.
// Two possible declarations for the swap method
public static <E> void swap(List<E> list, int i, int j);
public static void swap(List<?> list, int i, int j);
// Use this one instead of first one. But this one will not compile since the one above cannot set an element
// to the list object whose type is List<?> unless the object is null.
// And you should provide clean API list the code below.
public static void swap(List<?> list, int i, int j) {
swapHelper(list, i, j);
}
// Private helper method for wildcard capture
private static <E> void swapHelper(List<E> list, int i, int j) {
list.set(i, list.set(j, list.get(i)));
}
Summary
Using wildcard types in your APIs, while tricky, makes the APIs far more flexible. If you write a library that will be widely used, the proper use of wildcard types should be considered mandatory. Remember the basic rule: producer-extends, consumer-super(PECS). And remember that all comparables and comparators are consumers.
Effective Java 28 Use bounded wildcards to increase API flexibility的更多相关文章
- 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》读书笔记 - 5.泛型
		
Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw ...
 - Effective Java 目录
		
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
 - 【Effective Java】阅读
		
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
 - Effective Java 第三版——28. 列表优于数组
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective Java 第三版——26. 不要使用原始类型
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective Java 第三版——30. 优先使用泛型方法
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective Java 第三版——33. 优先考虑类型安全的异构容器
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 - Effective Java 第三版——37. 使用EnumMap替代序数索引
		
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
 
随机推荐
- intellij 调试方法
			
intellij 调试方法 转自 http://www.th7.cn/Program/net/201410/296492.shtml
 - Android学习笔记(第一篇)编写第一个程序Hello World+Activity
			
PS:终于开始正式的搞Android了...无人带的一介菜鸟,我还是自己默默的努力吧... 学习内容: 1.编写第一个Hello World程序.. 学习Android,那么就需要有一个编译器来集 ...
 - 关于开发Windows服务程序容易搞混的地方!
			
在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...
 - 二元查找树转变成排序的双向链表之C#算法实现
			
此题为July在CSDN发布的微软编程面试100题中的第一题,觉得蛮有趣的,今天也拿过来玩玩,July的代码用的是C++实现,可能因为有指针的原因吧,感觉看起来相对比较容易理解整个的实现过程,而我,试 ...
 - access数据库多个left join示例
			
代码: /// <summary> /// 分类检索 查询selectname /// </summary> public static DataTable GetSelect ...
 - 安装win8、ubuntu双系统的过程
			
弄了一个晚上,终于完成了,之前是用虚拟机的,但是觉得不带劲,并且折腾来时菜鸟变大神的捷径,虽然现在还一直在爬坑.继续奋斗吧...王小二 首先是看 ubuntu 百度贴吧的安装帖子(http://tie ...
 - ActiveReports 9 新功能:可视化查询设计器(VQD)介绍
			
在最新发布的ActiveReports 9报表控件中添加了多项新功能,以帮助你在更短的时间里创建外观绚丽.功能强大的报表系统,本文将重点介绍可视化数据查询设计器,无需手动编写任何SQL语句,主要内容如 ...
 - 窗体==>>初始Windows程序
			
初识Windows程序 01.创建Windows程序(VS) 01.打开Visual Studio开发工具 02.选择"文件"→"新建"→"项目&qu ...
 - [Visual Studio Online] 移除Work Item(Feature、Backlog item、Task)
			
[Visual Studio Online] 移除Work Item(Feature.Backlog item.Task) 移除 项目的开发过程中,使用Visual Studio Online来做Sc ...
 - 如果一个游戏上面加一个透明层,js能不能实现 点击透明层的任意点 而正常玩游戏
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...