Java 中的集合
2024-03-10 16:21:33
a framework/architecture(a set of classes /interface) to store and manipulation group(single unit) of objcts.
存储和操作对象集合的框架。
sorting, searching, insert, delete, iterate etc.
支持排序、查询、插入、删除、遍历等操作。
many interfaces: List, Set, Queue, Dequeue etc.
包装了许多接口。
many classes: ArrayList, Vector, LinkedList, PriorityQueue, HashSet, TreeSet etc.
包装有很多种。
The core collection interfaces CollectionA collection represents a group of objects known as its elements.
代表对象的集合,对象是集合元素。
Seta collection that cannot contain duplicate elements. unordered, no duplicate, at most one null.
不能包含重复元素的集合。元素无序,不允许重复,最多一个null。
Listan ordered collection (sometimes called a sequence).
有序集合。
Queuea collection used to hold multiple elements prior to processing. FIFO.
先进先出。
Dequedoubled ended queue.
Mapan object that maps keys to values.
SortedSeta Set that maintains its elements in ascending order.
SortedMapa Map that maintains its mappings in ascending key order.
The collection hierarchy CollectionImplementationsCollection
Ordering
Random Access
Key-Value
Duplicate Elements
Null Element
Thread Safety
ArrayList
Y
Y
N
Y
Y
N
LinkedList
Y
N
N
Y
Y
N
HashSet
N
N
N
N
Y
N
TreeSet
Y
N
N
N
N
N
HashMap
N
Y
Y
N
Y
N
TreeMap
Y
Y
Y
N
N
N
Vector
Y
Y
N
Y
Y
Y
Hashtable
N
Y
Y
N
N
Y
Properties
N
Y
Y
N
N
Y
Stack
Y
N
N
Y
Y
Y
CopyOnWriteArrayList
Y
Y
N
Y
Y
Y
ConcurrentHashMap
N
Y
Y
N
N
Y
CopyOnWriteArraySet
N
N
N
N
Y
Y
java.util.SetA collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
java.util.HashSet无重复元素的无序集合,使用 HashMap 实现。
java.util.TreeSet底层数据结构是红黑树,Treset不仅保证了元素的独特性,也保证了元素的顺序。
java.util.LinkedHashSetHashSet+LinkedHashMap.
maintain insertion order, permit nulls.
底层数据结构是链表和哈希表,用来保证唯一的元素,用来保证元素的插入顺序,即FIFO(First Input First Output 先进先出)。
java.util.concurrent.CopyOnWriteArraySetSet 实现线程安全。
java.util.concurrent.ConcurrentSkipListSet类似于 TreeSet,但是线程安全。
Sorted, navigable, and concurrent.
java.util.List java.util.ArrayList数组实现了一个可以动态生长和减少的索引序列。
random access, add/remove expensive(shift), not ordered.
优点是适合随机搜索和遍历,缺点是不适合插入和删除。
动态扩容。
java.util.LinkedList一个有序的序列,可以有效地插入和删除任何位置的操作,实现双向链表。
sequence access,add/remove cheap(no shift), ordered.
优点是适合动态插入和删除元素,缺点是随机搜索和遍历速度相对较慢。
java.util.concurrent.CopyOnWriteArrayListA thread-safe variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.
java.util.Vector数组实现,线程同步。
like ArrayList, but synchronized, more methods.
java.util.Stackextends Vector, LIFO, more methods.
java.util.QueueA collection designed for holding elements prior to processing.
方法/处理方法
抛出异常
返回特殊值
一直阻塞
超时退出
插入方法
add(e)
offer(e)
put(e)
offer(e, time, unit)
移除方法
remove()
poll()
take()
poll(time, unit)
检查方法
element()
peek()
不可用
不可用
java.util.PriorityQueue基于优先级堆实现的无界优先级队列。
An unbounded priority queue based on a priority heap.
一种集合,可以有效地删除最小元素。
java.util.concurrent.ArrayBlockingQueue有界阻塞队列由数组支持。
A bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
java.util.concurrent.LinkedBlockingQueue可选的有界阻塞队列基于链接节点。
An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
java.util.concurrent.LinkedTransferQueueAn unbounded TransferQueue based on linked nodes. This queue orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time for some producer.
java.util.concurrent.ConcurrentLinkedQueueAn unbounded thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
java.util.DequeA linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue" and is usually pronounced "deck". Most Deque implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.
java.util.concurrent.BlockingDequeA Deque that additionally supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element.
java.util.ArrayDeque采用循环数组实现的双端队列。
java.util.LinkedBlockingDequeAn optionally-bounded blocking deque based on linked nodes.
java.util.concurrent.ConcurrentLinkedDequeAn unbounded concurrent deque based on linked nodes.
java.util.Map java.util.HashMap数组+链表+红黑树是存储键/值相关的数据结构。
unique key, dup values; allow null values and null keys.
动态扩容:容量大小,负载因子。
jdk8 对 HashMap 的优化。
java.util.EnumMap一个键值属于枚举类型的映射表
java.util.TreeMap有序排列键值的映射表可以排序
java.util.LinkedHashMap一种映射表,可以记住键/值的添加顺序
java.util.WeakHashMap垃圾回收器可以回收一个无用的地方的映射表
java.util.IdentityHashMap用==,而不是用equals比较键值的映射表
java.util.concurrent.ConcurrentHashMapA hash table supporting full concurrency of retrievals and high expected concurrency for updates.
java.util.concurrent.ConcurrentSkipListMapSorted, navigable, and concurrent.
java.util.HashTable线程安全。
synchonized, no nulls.
Comparator vs. ComparableComparable 可比较的。Comparator 比较器。
Comparable 如果一个类实现了排序接口 Comparable 接口,意味着“这种类型的支持排序”。而 Comparator 它是一个比较器。如果我们需要控制某一类的顺序,我们可以建立一个“这种比较器”进行排序。
Comparable 相当于“内部比较器”,而 Comparator 相当于“外部比较器”。
如果对象的排名需要基于自然顺序,则使用它 Comparable,如果需要对不同对象的属性进行排序,则使用它 Comparator.
Collections vs. ArraysArrsys 是数组的工具类。
Collections 是集合对象的工具类。
Wrapper ImplementationsSynchronization Wrappers
Unmodifiable Wrappers
集合扩展Apache Common Collections
Google Guava, com.google.common.collect
不可变集合
新集合类型
集合工具类
集合扩展工具类
Java 8 Collections API FeaturesStream API
Lambda Expression and Functional interfaces
参考资料Trail: Collections
Collections in Java - Everything You MUST Know
Comparable和Comparator在Java中的区别小结
阻塞队列,有边界队列,无边界队列