Set vs. Set<?>(转)
You may know that an unbounded wildcard Set<?> can hold elements of any type, and a raw type Set can also hold elements of any type. What is the difference between them?
Two facts about Set<?>
There are two facts about Set<?>:
Item 1: Since the question mark ? stands for any type. Set<?> is capable of holding any type of elements.
Item 2: Because we don't know the type of ?, we can't put any element into Set<?>
So a Set<?> can hold any type of element(Item 1), but we can't put any element into it(Item 2). Do the two statements conflict to each other? Of course they are not. This can be clearly illustrated by the following two examples:
Item 1 means the following situation:
//Legal Code |
Since Set<?> can hold any type of elements, we simply use Object in the loop.
Item 2 means the following situation which is illegal:
//Illegal Code |
Because we don't know the type of <?> exactly, we can not add any thing to it other than null. For the same reason, we can not initialize a set with Set<?>. The following is illegal:
//Illegal Code |
Set vs. Set<?>
What's the difference between raw type Set and unbounded wildcard Set<?>?
This method declaration is fine:
public static void printSet(Set s) {
|
because raw type has no restrictions. However, this will easily corrupt the invariant of collection.
In brief, wildcard type is safe and the raw type is not. We can not put any element into a Set<?>.
When Set<?> is useful?
When you want to use a generic type, but you don't know or care what the actual type the parameter is, you can use <?>[1]. It can only be used as parameters for a method.
For example:
public static void main(String[] args) {
|
Reference:
1. Bloch, Joshua. Effective java. Addison-Wesley Professional, 2008.
You May Also Like ...
Category >> Collections >> Generics >> Versus
http://www.programcreek.com/2013/12/raw-type-set-vs-unbounded-wildcard-set/
随机推荐
- 解决VTune错误.../lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by ...)
错误信息及出现情景: 在export环境变量LD_PRELOAD=$XTERN_ROOT/dync_hook/interpose.so后,再执行amplxe-gui,出现上述错误.新增的动态链接库对V ...
- 美国企业人事喜欢考的3道.Net经典笔试题
1..求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m 答: int Num = this.TextBox1.Text.ToString() ; int Sum = 0 ; ...
- C++学习之路—继承与派生(二):派生类的构造函数与析构函数
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 由于基类的构造函数和析构函数是不能被继承的,所以 ...
- js与DOM初步:访问html元素
1.DOM简介 DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句话说,这是表示和处理一个HTML或XML文 ...
- 关于Delphi XE2的FMX的一点点研究之消息篇
Delphi XE2出来了一阵子了,里面比较抢眼的东西,除了VCLStyle这个换肤的东西之外,另外最让人眼亮的应该是FMX这个东西了.万一的博客上都连载了一票的关于FMX的使用心得了.我还是没咋去关 ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)
原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...
- RBAC用户角色权限设计方案
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用 户-角色 ...
- 14.4.3.1 The InnoDB Buffer Pool
14.4.3.1 The InnoDB Buffer Pool 14.4.3.2 Configuring Multiple Buffer Pool Instances 14.4.3.3 Making ...
- java之jvm学习笔记四(安全管理器)
java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...