java.util.Collection

  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque

image.png

  1. /**
  2. * Returns the number of elements in this collection. If this collection
  3. * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
  4. * <tt>Integer.MAX_VALUE</tt>.
  5. *
  6. * @return the number of elements in this collection
  7. */
  8. int size(); size最大大小Integer.MAX_VALUE

HashSet有序?
HashSet底层是HashMap 值是new Object();

  1. static final int hash(Object key) {
  2. int h;
  3. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  4. }
  5. public int hashCode() {
  6. int h = hash;
  7. if (h == 0 && value.length > 0) {
  8. char val[] = value;
  9. for (int i = 0; i < value.length; i++) {
  10. h = 31 * h + val[i];
  11. }
  12. hash = h;
  13. }
  14. return h;
  15. }

image.png
image.png

  1. LinkedList list = new LinkedList();
  2. Queue queue = new LinkedList();
  3. Deque deque = new LinkedList();

java.util.concurrent.BlockingQueue
java.util.concurrent.BlockingDeque
java.util.concurrent.TransferQueue

https://www.jrebel.com/blog/java-collections-cheat-sheet

java.util.HashMap

遗留实现

  • Vector

数组实现,线程安全

  • Stack
  • Hashtable

key value都不允许null

  • Enumeration

StringTokenizer

  • BitSet

image.png

抽象实现

AbstractCollection
AbstractList

面试题

DeepCloneDemo

Collection是单纬度数据集合
Map是二维数据集合

Java集合遍历实现

  • java.util.Collections
  • java.util.Arrays

Collections.jpg
Objects.png

不变集合

Collections.SingletonList();
singletonMap
singletonList

不变数组

需要深拷贝

空集合接口

  • Collections.emptyEnumeration()
  • Collections.emptyIterator()
  • Collections.emptyListIterator()

    转换接口

  • List Collections.list(Enumeration) Arrays.asList(T …)

  • Set Collections.newSetFromMap
  • Queue Collections.asLifoQueue(Deque)
  • HashCode Arrays.hashCode()
  • String Arrays.toString

    列举集合

  • BitSet.valueOf

  • EnumSet.valueOf
  • Stream.of
  • Set.of Since Java9

    包装实现

  • Collections.synchronized*

  • Collections.unmodifiable*
  • Collections.checked*

集合特殊实现

  • WeakHashMap
  • ThreadLocal.ThreadLocalMap
  • IdentityHashMap
  • PriorityQueue
  • EnumSet