一些关于集合内部算法可以查阅这篇文章《容器类总结》。
(Abstract+)
Collection
增:
- add(E):boolean
- addAll(Collection<? extends E>):boolean
删:
- remove(Object):boolean
- removeAll(Collection<?>):boolean
- retainAll(Collection<?>):boolean
- removeIf(Predicate<? super E>):boolean
- clear()
查:
- contains(Object):boolean
- containsAll(Collection<?>):boolean
- size():int
- isEmpty():boolean
- toArray():Object[]
- toArray(T[]):T[]
- Collections.max(Collection<? extends T>):T
- Collections.max(Collection<? extends T>, Comparator<? super T>):T
求最大值 - Collections.min(Collection<? extends T>):T
- Collections.min(Collection<? extends T>, Comparator<? super T>):T
求最小值
工具:
- iterator():Iterator<E>
- spliterator():Spliterator<E>
- stream():Stream<E>
- parallelStream():Stream<E>
- Collections.unmodifiableCollection(Collection<? extends T>):Collection<T>
只读模式包装 - Collections.synchronizedCollection(Collection<T>):Collection<T>
线程安全包装
(Abstract+)
(AbstractSequentia+)
List
父类:Collection
子类:ArrayList,LinkedList,Vector,Stack
增:
- add(int, E)
- addAll(int, Collection<? extends E>):boolean
删:
- remove(int):E
改:
- set(int, E):E
- replaceAll(UnaryOperator<E>)
- sort(Comparator<? super E>)
- Collections.swap(List<?>, int, int)
交换两元素位置
查:
- get(int):E
- indexOf(Object):int
- lastIndexOf(Object):int
- subList(int, int):List<E>
子集
工具:
- listIterator():ListIterator<E>
- listIterator(int):ListIterator<E>
- Collections.unmodifiableList(List<? extends T>):List<T>
只读模式包装 - Collections.synchronizedList(List<T>):List<T>
线程安全包装
ArrayList
父类:List,Collection
改:
- ensureCapacity(int)
- trimToSize()
LinkedList
父类:List,Deque,Queue,Connection
特征:链式存储
Vector
父类:List,Collection
子类:Stack
特征:线程安全
增:
- addElement(E)
- insertElementAt(E, int)
删:
- removeAllElements()
- removeElement(Object):boolean
- removeElementAt(int)
改:
- setElementAt(E, int)
- setSize(int)
- ensureCapacity(int)
- trimToSize()
查:
- firstElement():E
- lastElement():E
- elementAt(int):E
- elements():Enumeration<E>
- indexOf(Object, int):int
- lastIndexOf(Object, int):int
- capacity():int
- copyInto(Object[])
Stack
父类:Vector,List,Collection
特征:线程安全;先入后出
增:
- push(E):E
删:
- pop():E
改:
- peek():E
- search(Object):int
- empty():boolean
(Abstract+)
Queue
父类:Collection
子类:Deque,ConcurrentLinkedQueue,BlockingQueue,LinkedList
增:
- offer(E):boolean
删:
- poll():E
- remove():E
查:
- peek():E
- element():E
ConcurrentLinkedQueue
父类:Queue,Collection
特征:链式存储;线程安全;先进先出
BlockingQueue
父类:Queue,Collection
子类:ArrayBlockingQueue,LinkedBlockingQueue,PriorityBlockingQueue,DelayQueue,
SynchronousQueue,TransferQueue,LinkedTransferQueue,BlockingDeque
增:
- offer(E, long, TimeUnit):boolean
- put(E)
删:
- poll(long, TimeUnit):E
- take():E
- drainTo(Collection<? super E>):int
- drainTo(Collection<? super E>, int):int
查:
- remainingCapacity():int
ArrayBlockingQueue
父类:BlockingQueue,Queue,Collection
特征:线程安全;先进先出;队满时阻塞进队线程;队空时阻塞出队线程;
LinkedBlockingQueue
父类:BlockingQueue,Queue,Collection
特征:链式存储;线程安全;先进先出;队满时阻塞进队线程;队空时阻塞出队线程;
PriorityBlockingQueue
父类:BlockingQueue,Queue,Collection
特征:线程安全;策略排序;队空时阻塞出队线程;
查:
- comparator():Comparator<? super E>
DelayQueue<E extends Delayed>
父类:BlockingQueue,Queue,Collection
特证:线程安全;策略排序;队空时或队头未到期时,阻塞出队线程;
SynchronousQueue
父类:BlockingQueue,Queue,Collection
特征:线程安全;先进后出;元素进队(put)后,阻塞进队线程,至元素出队为止;队空时阻塞出队线程;
TransferQueue
父类:BlockingQueue,Queue,Collection
增:
- transfer(E)
- tryTransfer(E):boolean
- tryTransfer(E, long, TimeUnit):boolean
查:
- getWaitingConsumerCount():int
- hasWaitingConsumer():boolean
LinkedTransferQueue
父类:TransferQueue,BlockingQueue,Queue,Collection
特征:线程安全;先进先出;元素进队(transfer)后,阻塞进队线程,至元素出队为止;队空时阻塞出队线程;
Deque
父类:Queue,Collection
子类:ArrayDeque,BlockingDeque,LinkedList
增:
- addFirst(E)
- addLast(E)
- offerFirst(E):boolean
- offerLast(E):boolean
- push(E):void
删:
- removeFirst():E
- removeLast():E
- removeFirstOccurrence(Object):boolean
- removeLastOccurrence(Object):boolean
- pollFirst():E
- pollLast():E
- pop():E
查:
- getFirst():E
- getLast():E
- peekFirst():E
- peekLast():E
工具:
- descendingIterator():Iterator<E>
ArrayDeque
父类:Deque,Queue,Collection
BlockingDeque
父类:Deque,BlockingQueue,Queue,Collection
增:
- offerFirst(E, long, TimeUnit):boolean
- offerLast(E, long, TimeUnit):boolean
- putFirst(E)
- putLast(E)
删:
- pollFirst(long, TimeUnit):E
- pollLast(long, TimeUnit):E
- takeFirst():E
- takeLast():E
LinkedBlockingDeque
父类:BlockingDeque,BlockingQueue,Deque,Queue,Collection
(Abstract+)
Set
父类:Collection
子类:HashSet,TreeSet,SortedSet,NavigableSet,EnumSet
特征:代表不可重复序列
工具:
- Collections.unmodifiableSet(Set<? extends T>):Set<T>
只读模式包装 - Collections.synchronizedSet(Set<E>):Set<E>
线程安全包装
HashSet
父类:Set,Collection
特征:不可重复;策略排序(哈希表算法),一般代表无序
LinkedHashSet
父类:HashSet,Set,Collection
特征:不可重复;保留加入序
SortedSet
父类:Set,Collection
特征:代表有序
查:
- first():E
- last():E
- headSet(E):SortedSet<E>
- tailSet(E):SortedSet<E>
- subSet(E, E):SortedSet<E>
- comparator():Comparator<? super E>
工具:
- Collections.synchronizedSortedSet(SortedSet<T>):SortedSet<T>
线程安全包装 - Collections.unmodifiableSortedSet(SortedSet<T>):SortedSet<T>
只读模式包装
NavigableSet
子类:TreeSet
特征:提供高级操作
删:
- pollFirst():E
- pollLast():E
查:
- ceiling(E):E
- floor(E):E
- higher(E):E
- lower(E):E
- headSet(E, boolean):NavigableSet<E>
- tailSet(E, boolean):NavigableSet<E>
- subSet(E, boolean, E, boolean):NavigableSet<E>
- descendingSet():NavigableSet<E>
工具:
- descendingIterator():Iterator<E>
- Collections.unmodifiableNavigableSet(NavigableSet<T>):NavigableSet<T>
只读模式包装 - Collections.synchronizedNavigableSet(NavigableSet<T>):NavigableSet<T>
线程安全包装
TreeSet
父类:NavigableSet,SortedSet,Set,Collection
特征:不可重复;策略排序(红黑树算法);不能有空元素
EnumSet<E extends Enum<E>>
父类:Set,Collection
特征:用于枚举类型
构建:
- s+allOf(Class<E>):ElemSet<E>
全集 - s+noneOf(Class<E>):ElemSet<E>
空集 - s+copyOf(Collection<E>):ElemSet<E>
- s+copyOf(EnumSet<E>):ElemSet<E>
- s+complementOf(EnumSet<E>):ElemSet<E>
补集 - s+of(E...):ElemSet<E>
- s+range(E, E):ElemSet<E>
待续更新!