Set vs. Set(转)

时间:2023-01-09 21:56:26

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
public static void main(String[] args) {
HashSet<Integer> s1 = new HashSet<Integer>(Arrays.asList(1, 2, 3));
printSet(s1);
 
HashSet<String> s2 = new HashSet<String>(Arrays.asList("a", "b", "c"));
printSet(s2);
}
 
public static void printSet(Set<?> s) {
for (Object o : s) {
System.out.println(o);
}
}

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
public static void printSet(Set<?> s) {
s.add(10);//this line is illegal
for (Object o : s) {
System.out.println(o);
}
}

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<?> set = new HashSet<?>();

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) {
s.add("2");
for (Object o : s) {
System.out.println(o);
}
}

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) {
HashSet<Integer> s1 = new HashSet<Integer>(Arrays.asList(1,2,3));
HashSet<Integer> s2 = new HashSet<Integer>(Arrays.asList(4,2,3));
 
System.out.println(getUnion(s1, s2));
}
 
public static int getUnion(Set<?> s1, Set<?> s2){
int count = s1.size();
for(Object o : s2){
if(!s1.contains(o)){
count++;
}
}
return count;
}

Reference:

1. Bloch, Joshua. Effective java. Addison-Wesley Professional, 2008.

 
 
 

Category >> Collections >> Generics >> Versus

http://www.programcreek.com/2013/12/raw-type-set-vs-unbounded-wildcard-set/

随机推荐

  1. React的Diff算法

    使用React或者RN开发APP如果不知道Diff算法的话简直是说不过去啊.毕竟"知其然,知其所以然"这句老话从远古喊到现代了. 以下内容基本是官网文章的一个总结.压缩.这次要谦虚 ...

  2. 重命名Administrator账号

    (Get-WmiObject -class Win32_UserAccount | where {$_.SID -Like 'S-1-5-*-500'}).Rename("Ultraman& ...

  3. Panopticon跨平台的逆向工程反汇编工具

    http://www.freebuf.com/sectool/104045.html Panopticon 使用GPLv3授权许可,其免费. 项目文档:https://panopticon.re. 问 ...

  4. QuickSort 递归 分治

    QuickSort 参考<算法导论>,<C程序设计语言> #include<stdio.h> void swap(int v[], int i, int j); v ...

  5. Nancy入门

    Nancy入门 当我们要接到一个新的项目的时候,我们第一时间想到的是用微软的MVC框架,但是你是否想过微软的MVC是不是有点笨重?我们这个项目用MVC是不是有点大材小用?有没有可以替代MVC的东西呢? ...

  6. innodb更改行格式,系统盘占用急剧升高

    #大表引擎修改后,数据量较myisam引擎表大很多,对存储的行格式修改后,数据量减小. #备库修改时,由于服务器时间较早,系统盘20G,突然收到/磁盘空间占比89%的报警,立即将修改中断,恢复正常 # ...

  7. Alpha冲刺第十二天

    Alpha冲刺第十二天 站立式会议 项目进展 项目核心功能,如学生基本信息管理模块,学生信用信息模块,奖惩事务管理模块等等都已完成,测试工作大体结束. 问题困难 项目结束后对项目的阶段性总结缺乏一定的 ...

  8. 理解WebKit和Chromium&colon; Chromium插件和扩展基础

    转载请注明原文地址:http://blog.csdn.net/milado_nju ##概述 插件和扩展是一种扩充浏览器功能的技术,在之前我们介绍过NPAPI插件技术,在Chromium中,远远不只是 ...

  9. 行为驱动:Cucumber &plus; Selenium &plus; Java&lpar;五&rpar; - 使用maven来实现cucumber测试和报告

    在上一篇中,我们介绍了Selenium + Cucumber + Java框架下的测试用例参数化/数据驱动,这一篇我们来使用maven去搭建cucumber框架以及实现测试报告. 5.1 为什么要用m ...

  10. 自定义简单的模板引擎-JS模板引擎

    http://www.cnblogs.com/52fhy/p/5393673.html