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/
随机推荐
- 一种根据URL参数条件动态生成URL的方法
最近做了一个产品列表页类似于搜索列表页, 功能比较简单,比搜索页复杂的逻辑在于,生成各个查询条件的URL.我们的链接如下: http://xxx.xxx.xxx/product/list.html?s ...
- Twenty Newsgroups Classification任务之二seq2sparse(2)
接上篇,SequenceFileTokenizerMapper的输出文件在/home/mahout/mahout-work-mahout0/20news-vectors/tokenized-docum ...
- MVC3和MVC4中CRUD操作
MVC3中EF实现的CRUD操作 public class HomeController : Controller { // // GET: /Home/ CarModelContainer db = ...
- NET Core RC2 and .NET Core SDK Preview
NET Core RC2 and .NET Core SDK Preview 先看一下 .NET Core(包含 ASP.NET Core)的路线图: Beta6: 2015年7月27日 Beta7: ...
- bat执行java程序的脚本解析
使用java执行带Package的class文件java package1.package2.className 或java -cp . package1.package2.className - ...
- c++ :: 域操作符
c++ :: 域操作符 作用域:变量在程序中的起作用范围简单分为:全局作用域,局部作用域,语句作用域作用域优先级:范围越小优先级越高作用域运算符:"::" 如果希望在局部变量的作用 ...
- 为cocos2dx添加ndk库
碰到很多坑: 1:引用库定义成include $(BUILD_SHARED_LIBRARY),结果生成了两个so文件,应该把库声明为BUILD_STATIC_LIBRARY 2:把库的java放到了项 ...
- 【COCOS2DX-游戏开发之三三】TMX边界控制与小窗体内预览TMX
做一款像素游戏,须要确定地图的边界.保证人物的位置位于屏幕中央.到达地图左边界.地图位置不变.人向左走,到达右边界,地步位置不变,人向右走 如:地图左边.右边,上边空出的边界.还有下方留出操作butt ...
- Suse 创建NFS共享目录
Suse 创建NFS共享目录 服务端的配置: 1.编辑nfs服务的配置文件 /software/suse11 *(rw,sync,no_root_squash,no_all_squash) 凝视: / ...
- ThinkPhp学习08
原文:ThinkPhp学习08 一.普通查询方式 a.字符串 $arr=$m->where("sex=0 and username='gege'")->find(); ...