Set接口
声明
public interface Set<E> extends Collection<E>
接口定义的方法:
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean retainAll(Collection<?> c);
boolean removeAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
default Spliterator<E> spliterator();
除此之外还有两个跟排序有关的接口:
SortedSet:
//声明
public interface SortedSet<E> extends Set<E>
//新增方法
Comparator<? super E> comparator();
SortedSet<E> subSet(E fromElement, E toElement);
SortedSet<E> headSet(E toElement);
SortedSet<E> tailSet(E fromElement);
E first();
E last();
default Spliterator<E> spliterator();
NavigableSet:
//声明
public interface NavigableSet<E> extends SortedSet<E>
//新增方法
E lower(E e);
E floor(E e);
E ceiling(E e);
E higher(E e);
E pollFirst();
E pollLast();
Iterator<E> iterator();
NavigableSet<E> descendingSet();
Iterator<E> descendingIterator();
NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive);
NavigableSet<E> headSet(E toElement, boolean inclusive);
NavigableSet<E> tailSet(E fromElement, boolean inclusive);
可见实现了这两个接口的集合元素是有序的,并且要提供一系列排序相关的方法
Set类
抽象类AbstractSet继承了AbstractCollection
集合类HashSet继承了AbstractSet,实现了Set接口
集合类LinkedHashSet继承了HashSet,实现了Set接口
集合类TreeSet继承了AbstractSet,实现了NavigableSet接口
集合类EnumSet继承了AbstractSet
HashSet源码分析
声明:
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable
private transient HashMap<E,Object> map;
此为HashSet的底层数据结构,可见HashSet是基于HashMap的,并且元素对应为HashMap中的key
private static final Object PRESENT = new Object();
HashMap中的value都使用这个默认Object
看构造方法:
public HashSet();
public HashSet(Collection<? extends E> c);
public HashSet(int initialCapacity, float loadFactor);
public HashSet(int initialCapacity);
//这个方法被子类LinkedHashSet调用,初始化一个LinkedHashMap
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
看其他方法:
public Iterator<E> iterator();
public int size();
public boolean isEmpty();
public boolean contains(Object o);
public boolean add(E e);
public boolean remove(Object o);
public void clear();
public Object clone();
public Spliterator<E> spliterator();
综上可知,HashSet底层基于HashMap实现,元素对应于HashMap中的key,元素不可重复,同HashMap一样是无序集合
LinkedHashSet源码分析
声明:
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, java.io.Serializable
看构造方法:
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
}
public LinkedHashSet() {
super(16, .75f, true);
}
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true);
addAll(c);
}
这些构造方法都是调用HashSet中的构造方法初始化了一个LinkedHashMap
继承了HashSet的所有方法
综上可知,LinkedHashSet底层基于LinkedHashMap实现,元素不可重复,是有序集合,记录了元素的插入顺序,用迭代器遍历时是按元素插入顺序遍历的
TreeSet源码分析
声明:
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable
实现了NavigableSet,所以是有序集合,要有一系列排序有关的方法
private transient NavigableMap<E,Object> m;
底层的数据结构
看构造方法:
public TreeSet() {this(new TreeMap<E,Object>());}
public TreeSet(Comparator<? super E> comparator) {this(new TreeMap<>(comparator));}
public TreeSet(Collection<? extends E> c);
public TreeSet(SortedSet<E> s);
可见TreeSet底层实际上是基于TreeMap实现的
看其他方法:
public Iterator<E> iterator();
public Iterator<E> descendingIterator();
public NavigableSet<E> descendingSet();
public int size();
public boolean isEmpty();
public boolean contains(Object o);
public boolean add(E e);
public boolean remove(Object o);
public void clear();
public boolean addAll(Collection<? extends E> c);
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive);
public NavigableSet<E> headSet(E toElement, boolean inclusive);
public NavigableSet<E> tailSet(E fromElement, boolean inclusive);
public SortedSet<E> subSet(E fromElement, E toElement);
public SortedSet<E> headSet(E toElement);
public SortedSet<E> tailSet(E fromElement);
public Comparator<? super E> comparator();
public E first();
public E last();
public E lower(E e);
public E floor(E e);
public E ceiling(E e);
public E higher(E e);
public E pollFirst();
public E pollLast();
public Object clone();
public Spliterator<E> spliterator();
综上可知,TreeSet底层基于TreeMap实现,元素对应于TreeMap的key,故是有序集合,元素不可重复,根据指定比较器或自然序进行排序,并有一系列的排序相关的方法
EnumSet源码分析
声明:
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, java.io.Serializable
没有构造方法,可以用静态方法返回一个实例:
public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType);
public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType);
public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s);
public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c);
public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s);
public static <E extends Enum<E>> EnumSet<E> of(E e);
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2);
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3);
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4);
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5);
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest);
public static <E extends Enum<E>> EnumSet<E> range(E from, E to);
其他方法:
public EnumSet<E> clone();
//除此之外还继承了AbstractSet的所有其他方法
综上可知,EnumSet必须存储Enum类型的值,故底层是基于位向量的,元素不能重复,各种操作的效率都很高