Collection接口下的结构
目录
LinkedHashSet(父类HashSet,底层Map为LinkedHashMap)
①public boolean offer(E e) / public boolean add(E e)
addAll(int index, Collection c):
②public boolean remove(Object o)
②public E set(int index, E element)
③public int indexOf(Object o) :可以查找null元素,意味ArrayList可以存放null
⑤public E remove(int index):删除时会移动大量元素
Vector(过时的类,每个方法都有Synchronized)[可以存null]
Set
HashSet
一些属性
*所有的键都有同一个值PRESENT
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
//用作所有键的值,因为HashSet中只存键不存值
private static final Object PRESENT = new Object();
}
方法都是调用HashMap中的方法,不再重复
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean contains(Object o) {
return map.containsKey(o);
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
public void clear() {
map.clear();
}
LinkedHashSet(父类HashSet,底层Map为LinkedHashMap)
一些属性
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
//没有属性,都是调用父类的属性
}
构造函数
调用父类的构造函数,使其底层实现变成LinkedHashMap
//两个方法调用同一个父类的构造方法
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(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
TreeSet(依赖TreeMap)
一些属性
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable
{
/**
* The backing map.
*/
private transient NavigableMap<E,Object> m;
// Dummy value to associate with an Object in the backing Map
//同样的套路:所有键的value都是PRESENT
private static final Object PRESENT = new Object();
}
构造函数
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}
public TreeSet() {
this(new TreeMap<E,Object>());
}
//还有别的构造方法就不一一列举了
*底层方法实现同TreeMap
public int size() {
return m.size();
}
public boolean isEmpty() {
return m.isEmpty();
}
public boolean contains(Object o) {
return m.containsKey(o);
}
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return m.remove(o)==PRESENT;
}
public void clear() {
m.clear();
}
ConcurrentSkipListSet
一些属性
构造函数
①
②
List-Queue
PriorityQueue(默认小顶堆)
①public boolean offer(E e) / public boolean add(E e)
public boolean add(E e) {
return offer(e);
}
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
modCount++;
int i = size;
if (i >= queue.length)
grow(i + 1);
size = i + 1;
if (i == 0)
queue[0] = e;
else
siftUp(i, e);
return true;
}
/**
* Increases the capacity of the array.
*
* @param minCapacity the desired minimum capacity
*/
//数组容量已经不满足下标了,遂扩容
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
//如果原本容量小于64就变2n+2;否则变1.5n
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
(oldCapacity + 2) :
(oldCapacity >> 1));
// overflow-conscious code
//数组最大值 or Integer_MAX_VALUE
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
queue = Arrays.copyOf(queue, newCapacity);
}
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
//有比较器(默认小顶堆) k为x在数组中的下标
@SuppressWarnings("unchecked")
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
//父亲结点数组下标
int parent = (k - 1) >>> 1;
Object e = queue[parent];
//比父亲大,小顶堆,就不用在往上移了
if (comparator.compare(x, (E) e) >= 0)
break;
//暂时不用赋值x,最后循环出去再赋值
queue[k] = e;
k = parent;
}
queue[k] = x;
}
//Queue没有比较器,使用插入的对象的比较器
@SuppressWarnings(