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/
随机推荐
- twitter分享问题(四)—— Unknown error(api v1过度到V1.1产生)
unknow error! 今天为了使用GA(谷歌分析)追踪twitter分享,又测试了一下twitter分享功能,发现又出问题(使用sharekit分享).就是“unknow error”,之前也碰 ...
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...
- RTSP 消息拼装实例代码
整理了下之前文章提到的RTSP实例的代码,主要是拼装各类消息字段. 首先,抽取一个公共函数,用来根据消息类型,生成不同的format,供sprintf试用. char * GetRTSPCmd( co ...
- TCP_NODELAY详解
在网络拥塞控制领域,我们知道有一个非常有名的算法叫做Nagle算法(Nagle algorithm),这是使用它的发明人John Nagle的名字来命名的,John Nagle在1984年首次用这个算 ...
- struts 2吊牌s:if 、s:iterator注意
疏忽,也没有相应的总结.实际上JSTL标签Struts2标签混淆.导致一些上述问题的细节.今天我给从下一个总结,同 后不要再犯这种错误. 总喜欢在s:if标签里面使用$,导致各种数据读不出来. str ...
- iOS高效开源类库
因为iOS SDK相对比较底层,所以开发者就得受累多做一些体力活.不过幸运的是,有很多第三方的类库可以用来简化很多不必要的工作.笔者整理了一下在本人学习过程中用到的一些比较有用Objective-C开 ...
- 打开asp出现An error occurred on the server when processing the URL
分享到: 2013-01-21 15:38 提问者采纳 方法一 以管理员身份运行CMD,将目录定位到%windir%\system32\inetsrv\,然后执行appcmd set co ...
- Functional Jobs // Hire Functional Programmers In Less Time
Functional Jobs // Hire Functional Programmers In Less Time Hire Functional Programmers Quick Save T ...
- hdu4055 Number String
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- activity_main.xml: java.lang.NullPointerException
1.错误描写叙述 eclipse.buildId=4.4.0.I20140606-1215 java.version=1.7.0_67 java.vendor=Oracle Corporation B ...