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/
随机推荐
- android 实现跳动频谱 DEMO
package com.terry.AudioFx; import android.app.Activity; import android.content.Context; import andro ...
- 《Swift编程语言》中文翻译及读书笔记page25
The Swift Programming Language读书笔记学习笔记 第25页 本页主要说在swift语言里能够使用分号,但分号不作为每条swift语言语句的结尾 而是间隔写在一行的多条swi ...
- IOS之【地图MapKit】
iOS地图位置开发 iPhone SDK提供了三个类来管理位置信息:CLLocation CLLocationManager 和 CLLHeading(不常用).除了使用GPS来获取当前的位置信息 ...
- VS2005 MFC 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
当 Visual C++ 项目启用了预编译头 (Precompiled header) 功能时,如果项目中同时混合有 .c 和 .cpp 源文件,则可能收到 C1853 编译器错误:fatal err ...
- 与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器
原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 [索引页][源码下载] 与众不同 windows phone (18) - Device ...
- lua 函数回调技巧
技巧1: local a = {}; function b() print("Hello World") end a["sell"] = {callFunc = ...
- Redis Destop Manager不能访问虚拟机
虚拟机centOS中安装Redis,主机Redis Destop Manager不能访问虚拟机Redis server的解决方案 今天在学些redis的时候碰到个问题,发现主机Redis Destop ...
- 与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast)
原文:与众不同 windows phone (32) - Communication(通信)之任意源组播 ASM(Any Source Multicast) [索引页][源码下载] 与众不同 wind ...
- LVS Nginx HAProxy 优缺点
搭建负载均衡高可用环境相对简单,主要是要理解其中原理.此文描述了三种负载均衡器的优缺点,以便在实际的生产应用中,按需求取舍. 目前,在线上环境中应用较多的负载均衡器硬件有F5 BIG-IP,软件有LV ...
- 14.3.2.1 Transaction Isolation Levels 事务隔离级别
14.3.2 InnoDB Transaction Model InnoDB 事务模型 14.3.2.1 Transaction Isolation Levels 事务隔离级别 14.3.2.2 au ...