深入理解HashSet

简要介绍

  • HashSet 是一个没有重复元素的集合。
  • 它是由HashMap实现的,不保证元素的顺序,而且HashSet允许使用 null 元素。
  • HashSet是非同步的。如果多个线程同时访问一个哈希 set,而其中至少一个线程修改了该 set,那么它必须
    保持外部同步。这通常是通过对自然封装该 set 的对象执行同步操作来完成的。如果不存在这样的对象,则应该使用
    Collections.synchronizedSet 方法来“包装” set。最好在创建时完成这一操作,以防止对该 set进行意外的不同步访问:

Set s = Collections.synchronizedSet(new HashSet(…));

源码

使用

public static void main(String[] args) {
        // HashSet常用API
        testHashSetAPIs() ;
    }

    /*
     * HashSet除了iterator()和add()之外的其它常用API
     */
    private static void testHashSetAPIs() {
        // 新建HashSet
        HashSet set = new HashSet();

        // 将元素添加到Setset.add("a");
        set.add("b");
        set.add("c");
        set.add("d");
        set.add("e");

        System.out.println("set:"+set);
        // 打印HashSet的实际大小
        System.out.println("set.size():"+set.size());

        // 判断HashSet是否包含某个值
        System.out.printf("HashSet contains a :%s\n", set.contains("a"));
        System.out.printf("HashSet contains g :%s\n", set.contains("g"));

        // 删除HashSet中的“e”
        System.out.println("set.remove(e):"+set.remove("e"));


        // 将Set转换为数组
        String[] arr = (String[])set.toArray(new String[0]);
        for (String str:arr)
            System.out.printf("for each : %s\n", str);

        // 新建一个包含b、c、f的HashSet
        HashSet otherset = new HashSet();
        otherset.add("b");
        otherset.add("c");
        otherset.add("f");
        System.out.println("otherset:"+otherset);

        // 克隆一个removeset,内容和set一模一样
        HashSet removeset = (HashSet)set.clone();
        System.out.println("before remove removeset:"+removeset);
        // 删除“removeset中,属于otherSet的元素”
        removeset.removeAll(otherset);
        // 打印removeset
        System.out.println("after otherset remove from removeset:"+removeset);

        // 克隆一个retainset,内容和set一模一样
        HashSet retainset = (HashSet)set.clone();
        System.out.println("before retainAll retainset:"+retainset);
        // 保留“retainset中,属于otherSet的元素”
        retainset.retainAll(otherset);
        // 打印retainset
        System.out.println("after otherset retainAll from retainset:"+retainset);


        // 遍历HashSet
        for(Iterator iterator = set.iterator();
               iterator.hasNext(); ) 
            System.out.printf("iterator : %s\n", iterator.next());

        // 清空HashSet
        set.clear();

        // 输出HashSet是否为空
        System.out.printf("%s\n", set.isEmpty()?"set is empty":"set is not empty");
    }

/*******************打印如下********************************
set:[a, b, c, d, e]
set.size():5
HashSet contains a :true
HashSet contains g :false
set.remove(e):true
for each : a
for each : b
for each : c
for each : d
otherset:[b, c, f]
before remove removeset:[a, b, c, d]
after otherset remove from removeset:[a, d]
before retainAll retainset:[a, b, c, d]
after otherset retainAll from retainset:[b, c]
iterator : a
iterator : b
iterator : c
iterator : d
set is empty
***********************************************************/

重点理解

1.HashSet的本质是一个”没有重复元素”的集合,它是通过HashMap实现的。HashSet中含有一个”HashMap类型的成员变量”map,HashSet的操作函数,实际上都是通过map实现的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值