Collection集合,collection
一些關於集合內部算法可以查閱這篇文章《容器類總結》。
(Abstract+)
Collection
子類:List,Queue,Set
增:
- 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
刪:
改:
- 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
特征:線程安全;先入後出
增:
刪:
改:
- peek():E
- search(Object):int
- empty():boolean
(Abstract+)
Queue
父類:Collection
子類:Deque,ConcurrentLinkedQueue,BlockingQueue,LinkedList
增:
刪:
查:
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
查:
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
子類:LinkedTransferQueue
增:
- 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
子類:LinkedBlockingDeque
增:
- 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
特征:不可重復;策略排序(哈希表算法),一般代表無序
LinkedHashSet
父類:HashSet,Set,Collection
特征:不可重復;保留加入序
SortedSet
父類:Set,Collection
子類:TreeSet,NavigableSet
特征:代表有序
查:
- 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
父類:SortedSet,Set,Collection
子類: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>
待續更新!