Java 集合框架實作:大O 總結
在教授「Java 速成課程」時,了解以下內容至關重要與各種集合實現相關的Big-O 時間複雜度。雖然觀眾可能熟悉 Big-O 表示法,但他們不太可能知道特定集合上不同操作的特定複雜性。
幸運的是,《Java 泛型和集合》一書提供了有關這些複雜性的寶貴見解。本文總結了書中的信息,為每種類型的集合提供了詳細的表格:列表、集合、映射和隊列。
列表實作:
Operation | ArrayList | LinkedList | CopyOnWriteArrayList |
---|---|---|---|
get | O(1) | O(n) | O(1) |
add | O(1) | O(1) | O(n) |
contains | O(n) | O(n) | O(n) |
next | O(1) | O(1) | O(1) |
remove(0) | O(n) | O(1) | O(n) |
iterator.remove | O(n) | O(1) | O(n) |
集合實作:
Operation | HashSet | LinkedHashSet | CopyOnWriteArraySet | EnumSet | TreeSet | ConcurrentSkipListSet |
---|---|---|---|---|---|---|
add | O(1) | O(1) | O(n) | O(1) | O(log n) | O(log n) |
contains | O(1) | O(1) | O(n) | O(1) | O(log n) | O(log n) |
next | O(h/n) | O(1) | O(1) | O(1) | O(log n) | O(1) |
Notes | h is the table capacity |
實作地圖:
>Operation | HashMap | LinkedHashMap | IdentityHashMap | EnumMap | TreeMap | ConcurrentHashMap | ConcurrentSkipListMap |
---|---|---|---|---|---|---|---|
get | O(1) | O(1) | O(1) | O(1) | O(log n) | O(1) | O(log n) |
containsKey | O(1) | O(1) | O(1) | O(1) | O(log n) | O(1) | O(log n) |
next | O(h/n) | O(1) | O(h/n) | O(1) | O(log n) | O(h/n) | O(1) |
Notes | h is the table capacity |
隊列實作:
Operation | PriorityQueue | ConcurrentLinkedQueue | ArrayBlockingQueue | LinkedBlockingQueue | PriorityBlockingQueue | DelayQueue | LinkedList | ArrayDeque | LinkedBlockingDeque |
---|---|---|---|---|---|---|---|---|---|
offer | O(log n) | O(1) | O(1) | O(1) | O(log n) | O(log n) | O(1) | O(1) | O(1) |
peek | O(1) | O(1) | O(1) | O(1) | O(1) | O(1) | O(1) | O(1) | O(1) |
poll | O(log n) | O(1) | O(1) | O(1) | O(log n) | O(log n) | O(1) | O(1) | O(1) |
size | O(1) | O(n) | O(1) | O(1) | O(1) | O(1) | O(1) | O(1) | O(1) |
以上是文章的標題應該是:What are the Big-O Time Complexities of Java Collection Implements?的詳細內容。更多資訊請關注PHP中文網其他相關文章!