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方法。