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/
随机推荐
- Swift - 系统声音服务的使用(播放声音,提醒,震动)
1,系统声音服务介绍: 系统声音服务提供了一个Api,用于播放不超过30秒的声音.它支持的文件格式有限,具体的说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. 但此函数没有提供操 ...
- window2003远程桌面“已达最大连接数”
使用命令行强制注销远程登录用户 Fri, 04/19/2013 - 09:29 - admin 来源地址: http://space.itpub.net/10067101/viewspace-6147 ...
- 管道是如何处理HTTP请求的?
管道是如何处理HTTP请求的? 我们知道ASP.NET Core请求处理管道由一个服务器和一组有序的中间件组成,所以从总体设计来讲是非常简单的,但是就具体的实现来说,由于其中涉及很多对象的交互,我想很 ...
- Enterprise Solution 企业管理软件开发框架
Enterprise Solution 开源项目资源汇总 Visual Studio Online 源代码托管 企业管理软件开发框架 Enterprise Solution 是一套管理软件开发框架,在 ...
- TPanel的默认颜色存储在dfm中,读取后在Paint函数中设置刷子的颜色,然后填充整个背景
声明如下: TCustomPanel = class(TCustomControl) private FFullRepaint: Boolean; FParentBackgroundSet: Bool ...
- ext4 delalloc相关
ext4文件系统delayed allocation相关研究 最近在一个项目上测试录音时,发现有丢数据的现象.通过串口发现打出了很多overrun的log. overrun是驱动层给上层应用的一个通知 ...
- 将n进制的数组压缩成字符串(0-9 a-z)同一时候解压
比如一个3进制的数组: [1 1 2 2 2 0 0] 用一个字符串表示... 此类题目要明白两点: 1. 打表:用数组下标索引字符.同一时候注意假设从字符相应回数字: int index = (st ...
- 积累的VC编程小技巧之工具条和状态条
1.工具条和状态条中控件的添加: 方法⑴.只能在ToolBar里创建控件:首先,在ToolBar中创建一个Button,其ID为ID_TOOL_COMBO(我们要将创建的控件放在该Button的位置上 ...
- Delphi颜色的表示(一共5种表示法)
//全以红色举例: //1. RGB 模式:Self.Color := $0000ff; //不过和HTML.PhotoShop.FireWorks中的 #ff0000 是完全反的,应该叫 BGR. ...
- Shell printf 命令
Shell printf 命令 printf 命令模仿 C 程序库(library)里的 printf() 程序. 标准所定义,因此使用printf的脚本比使用echo移植性好. printf 使用引 ...