void linkLast(E e) { final Node<E> l = last; //获取尾部节点 final Node<E> newNode = new Node<>(l, e, null);//new 一个新的节点对象 last = newNode; if (l == null) //如果尾部节点为null,说明此list中还没有元素, first = newNode; else //如果尾部节点不为null,将last的下一个元素指向newNode l.next = newNode; size++; modCount++; }
push为头部添加
1 2 3 4 5 6 7 8 9 10 11
private void linkFirst(E e) { final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; }
HashSet
HashSet 是一个没有重复元素的集合。是 Set 集合的子类,实际却为 HashMap 的实例
1 2 3
public HashSet() { map = new HashMap<>(); }
HashSet是无序的,HashSet 默认容量为 16,每次扩充 0.75 倍
1 2 3 4
public HashSet(Collection<? extends E> c) { map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16)); addAll(c); }
HashSet通过map的key唯一来保证了hashSet中的值是唯一的
1 2 3
public boolean add(E e) { return map.put(e, PRESENT)==null; }