Set接口实现类HashSet源码及详解

1.HashSet简介

 

此类实现 Set接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。

 

 

2.HashSet源码

<strong>public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable {
    public HashSet() {
        throw new RuntimeException("Stub!");
    }

    public HashSet(Collection<? extends E> c) {
        throw new RuntimeException("Stub!");
    }

    public HashSet(int initialCapacity, float loadFactor) {
        throw new RuntimeException("Stub!");
    }

    public HashSet(int initialCapacity) {
        throw new RuntimeException("Stub!");
    }

    public Iterator<E> iterator() {
        throw new RuntimeException("Stub!");
    }

    public int size() {
        throw new RuntimeException("Stub!");
    }

    public boolean isEmpty() {
        throw new RuntimeException("Stub!");
    }

    public boolean contains(Object o) {
        throw new RuntimeException("Stub!");
    }

    public boolean add(E e) {
        throw new RuntimeException("Stub!");
    }

    public boolean remove(Object o) {
        throw new RuntimeException("Stub!");
    }

    public void clear() {
        throw new RuntimeException("Stub!");
    }

    public Object clone() {
        throw new RuntimeException("Stub!");
    }

    public Spliterator<E> spliterator() {
        throw new RuntimeException("Stub!");
    }
}</strong>

 

 

 

 

3.HashSet常用方法

 

3.1.size():集合的长度

<strong>HashSet<String> hashSet=new HashSet<String>();
int num=hashSet.size();</strong>

 

3.2.isEmpty():集合是否为空

<strong>HashSet<String> hashSet=new HashSet<String>();
boolean b=hashSet.isEmpty();</strong>

 

3.3.add(E e):向集合中添加元素

<strong> HashSet<String> hashSet=new HashSet<String>();
 hashSet.add(null);
 hashSet.add("ffff");</strong>

 

3.4.contains(Object o):集合中是否包含o元素

<strong>HashSet<String> hashSet=new HashSet<String>();
hashSet.add(null);
hashSet.add("ffff");
hashSet.add("aaaa");
boolean b=hashSet.contains("aaaa");
boolean c=hashSet.contains("AAAAAAAAAA");
boolean d=hashSet.contains(null);
Log.d("TAG","b----:"+b);
Log.d("TAG","c----:"+c);
Log.d("TAG","d----:"+d);</strong>
<strong>b----:true
c----:false
d----:true</strong>

 

3.5.remove(Object o):删除集合中o元素

<strong>HashSet<String> hashSet=new HashSet<String>();
hashSet.add(null);
hashSet.add("ffff");
hashSet.add("aaaa");
hashSet.add("aaaa");
hashSet.add("bbbb");
hashSet.add("bbbb");
hashSet.add("cccc");
hashSet.add("cccc");
hashSet.add("dddd");
hashSet.add(null);
int num1=hashSet.size();
Log.d("TAG","num1----:"+num1);
hashSet.remove("ffff");
int num2=hashSet.size();
Log.d("TAG","num2----:"+num2);</strong>
<strong>num1----:6
num2----:5</strong>

 

3.6.clear():清空集合所有元素

<strong>HashSet<String> hashSet=new HashSet<String>();
hashSet.add(null);
hashSet.add("ffff");
hashSet.add("aaaa");
hashSet.add("aaaa");
hashSet.add("bbbb");
hashSet.add("bbbb");
hashSet.add("cccc");
hashSet.add("cccc");
hashSet.add("dddd");
hashSet.add(null);
int num1=hashSet.size();
Log.d("TAG","num1----:"+num1);
hashSet.clear();
int num2=hashSet.size();
Log.d("TAG","num2----:"+num2);</strong>
<strong>num1----:6
num2----:0</strong>

 

3.7.iterator():遍历集合

<strong>HashSet<String> hashSet=new HashSet<String>();
hashSet.add(null);
hashSet.add("ffff");
hashSet.add("aaaa");
hashSet.add("aaaa");
hashSet.add("bbbb");
hashSet.add("bbbb");
hashSet.add("cccc");
hashSet.add("cccc");
hashSet.add("dddd");
hashSet.add(null);

Iterator<String> i=hashSet.iterator();
while(i.hasNext()){
  Log.d("TAG",i.next()+"");
}</strong>
<strong>null
cccc
ffff
dddd
bbbb
aaaa</strong>

 

 

 

4.总结

HashSet 无序 元素不可重复 元素可为空 。且没有get方法。

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值