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
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);
    }
}
</string></string></integer></integer>

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;
}
</integer></integer></integer></integer>

Reference:

Original: https://www.cnblogs.com/rollenholt/articles/4237790.html
Author: Rollen Holt
Title: Set vs. Set<?>

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/553330/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • C++ 虚基类与虚继承 (菱形继承问题)

    C++ 是支持多继承的语言, 但是实际项目开发中非必要不要使用多继承以降低代码逻辑的复杂性,当然 C++ 多继承的特性带来一些问题即 菱形继承。 当一个类继承了两个来自同父类的子类…

    技术杂谈 2023年6月21日
    093
  • typora 标题自动加编号

    网上看了挺多其他复制粘贴的方法,但是缺点都是第一级 h1 ,都算进去了 按照规范的 markdown 规范,一个 md 文件中只能出现一个 h1,就是标题 所以最大的编号应该就是 …

    技术杂谈 2023年7月11日
    068
  • [转帖]普通用户sudosu 到root免密码

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年5月30日
    078
  • image图片

    *image图片的私有属性 属性 说明 isCircle 是否圆图,true表示圆形,默认值false标示方形 url 图片的链接 <image url="…&…

    技术杂谈 2023年6月1日
    074
  • 如何保证消息消费的幂等性

    或者说,如何保证消息消费的幂等性? 业务场景 在本项目中,新增员工接口,会有邮件发送,在测试接口的过程中,我们可能会有重复增加的操作,相对应的消费者端会收到两个邮件。 但是我们的用…

    技术杂谈 2023年7月11日
    078
  • ps 创建文字模板的方法

    使用文字模板工具,注意不要使用移动工具移动选区,否则这样选区内的像素也会跟着移动,要使用选区工具移动和变换 Original: https://www.cnblogs.com/da…

    技术杂谈 2023年7月25日
    087
  • 西安律师起诉酷骑单车 要求”退一赔三”_网易新闻

    西安律师起诉酷骑单车 要求”退一赔三”_网易新闻 http://shanxi.news.163.com/17/1114/08/D36JEHDL04198EV…

    技术杂谈 2023年5月31日
    0127
  • MySQL — 索引

    索引(Index)是高效获取数据的数据结构,就像书的目录,提高检索数据的效率。 优点:提高数据检索效率,降低数据库的 IO 成本;通过索引列对数据进行排序,降低数据排序的成本,降低…

    技术杂谈 2023年7月11日
    060
  • 用R语言分析与预測员工离职

    作者简单介绍 糖甜甜甜,R语言中文社区专栏作者 公众号:经管人学数据分析 在实验室搬砖之后,继续我们的kaggle数据分析之旅,这次数据也是答主在kaggle上选择的比較火的一份关…

    技术杂谈 2023年5月31日
    0103
  • PyQt5 鼠标事件

    ################################ PyQt5中文网 – PyQt5全套视频教程 # https://www.PyQt5.cn/ # 主讲: 村长 #…

    技术杂谈 2023年5月31日
    085
  • 关于素数定理的一个延拓

    一直以来,我们总是在孜孜不倦地寻找素数的规律,但是,很难成功,我们可以把素数看作人类思想无法渗透的秘密. 公元前3世纪,古希腊哲学家Eratosthenes提出了一个叫&#8221…

    技术杂谈 2023年5月31日
    087
  • 不经意传输(OT)-总结

    https://zhuanlan.zhihu.com/p/399361005 Oblivious Transfer 总结 ​ 不经意传输(OT,oblivious transfer…

    技术杂谈 2023年5月31日
    095
  • 【转】iOS AVPlayer的那些坑

    这次主要是总结和记录下视频播放遇到的坑,视频播放采用的是AVPlayer这个控件,语法大致如下: 一般说来,这里要监听AVPlayerItem的status属性: 如果是AVPla…

    技术杂谈 2023年6月1日
    095
  • Switchhosts没有写入host文件的权限的解决方法

    第一次使用Switchhosts来修改host,记录一下遇到的小问题,这个通用的解决方法很多博客都记录了,浅浅说一下传统的解决思路,以及自己遇到的一个小tips! 通用解决方法: …

    技术杂谈 2023年7月11日
    081
  • jetbrain idea误删文件的两种恢复的方法

    idea误删文件的两种恢复的方法。第一种,ctrl+z这一种没必要多说什么,主要在书写代码的时候常用,它的即时性比较高。如果是过了很长时间或者软件关了在打开,那么它就没什么用处了。…

    技术杂谈 2023年5月31日
    080
  • Vim 练级攻略

    以下的文章翻译自《Learn Vim Progressively》,我认为这是给新手最好的VIM的升级教程了,没有列举全部的命令,仅仅是列举了那些最实用的命令。 很不错。 ————…

    技术杂谈 2023年5月31日
    098
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球