Skip to content

Instantly share code, notes, and snippets.

@iSergius
Created September 3, 2016 14:52
Show Gist options
  • Save iSergius/e06963c6eca0a639023666097227427c to your computer and use it in GitHub Desktop.
Save iSergius/e06963c6eca0a639023666097227427c to your computer and use it in GitHub Desktop.
Below are the Big O performance of common functions of different Java Collections.
List | Add | Remove | Get | Contains | Next | Data Structure
---------------------|------|--------|------|----------|------|---------------
ArrayList | O(1) | O(n) | O(1) | O(n) | O(1) | Array
LinkedList | O(1) | O(1) | O(n) | O(n) | O(1) | Linked List
CopyOnWriteArrayList | O(n) | O(n) | O(1) | O(n) | O(1) | Array
Set | Add | Remove | Contains | Next | Size | Data Structure
----------------------|----------|----------|----------|----------|------|-------------------------
HashSet | O(1) | O(1) | O(1) | O(h/n) | O(1) | Hash Table
LinkedHashSet | O(1) | O(1) | O(1) | O(1) | O(1) | Hash Table + Linked List
EnumSet | O(1) | O(1) | O(1) | O(1) | O(1) | Bit Vector
TreeSet | O(log n) | O(log n) | O(log n) | O(log n) | O(1) | Red-black tree
CopyOnWriteArraySet | O(n) | O(n) | O(n) | O(1) | O(1) | Array
ConcurrentSkipListSet | O(log n) | O(log n) | O(log n) | O(1) | O(n) | Skip List
Queue | Offer | Peak | Poll | Remove | Size | Data Structure
------------------------|----------|------|----------|--------|------|---------------
PriorityQueue | O(log n) | O(1) | O(log n) | O(n) | O(1) | Priority Heap
LinkedList | O(1) | O(1) | O(1) | O(1) | O(1) | Array
ArrayDequeue | O(1) | O(1) | O(1) | O(n) | O(1) | Linked List
ConcurrentLinkedQueue | O(1) | O(1) | O(1) | O(n) | O(n) | Linked List
ArrayBlockingQueue | O(1) | O(1) | O(1) | O(n) | O(1) | Array
PriorirityBlockingQueue | O(log n) | O(1) | O(log n) | O(n) | O(1) | Priority Heap
SynchronousQueue | O(1) | O(1) | O(1) | O(n) | O(1) | None!
DelayQueue | O(log n) | O(1) | O(log n) | O(n) | O(1) | Priority Heap
LinkedBlockingQueue | O(1) | O(1) | O(1) | O(n) | O(1) | Linked List
Map | Get | ContainsKey | Next | Data Structure
----------------------|----------|-------------|----------|-------------------------
HashMap | O(1) | O(1) | O(h / n) | Hash Table
LinkedHashMap | O(1) | O(1) | O(1) | Hash Table + Linked List
IdentityHashMap | O(1) | O(1) | O(h / n) | Array
WeakHashMap | O(1) | O(1) | O(h / n) | Hash Table
EnumMap | O(1) | O(1) | O(1) | Array
TreeMap | O(log n) | O(log n) | O(log n) | Red-black tree
ConcurrentHashMap | O(1) | O(1) | O(h / n) | Hash Tables
ConcurrentSkipListMap | O(log n) | O(log n) | O(1) | Skip List
@benoitantelme
Copy link

You've mixed data structures between LinkedList and ArrayDequeue in the Queue section.

@Cpt-xx
Copy link

Cpt-xx commented Mar 11, 2021

Do these figures still hold in the new Java versions? If not, then I would suggest adding the Java-version these performances were measured at.

I presume by and large the performance does not change dramatically though.

@ArthurSchiavom
Copy link

Do these figures still hold in the new Java versions? If not, then I would suggest adding the Java-version these performances were measured at.

I presume by and large the performance does not change dramatically though.

I highly doubt this was measured. It's probably inferred from the implementation. And I doubt the complexity of the implementations changed. Most algorithms have been around for a long time.

@AndiCover
Copy link

The complexity of adding an element to an ArrayList should be O(n) because in the worst case the underlying array is full and you need to expand it --> Copy all elements in a larger array.

@ali-tunahan
Copy link

Complexity of adding an element to an ArrayList should be O(1)+ (amortized time notation).

@kawsarahmedbhuiyan
Copy link

Queue Offer Peak Poll Remove Size Data Structure
LinkedList O(1) O(1) O(1) O(1) O(1) Array
ArrayDequeue O(1) O(1) O(1) O(n) O(1) Linked List

You have made a mistake here. The underlying data structure for LinkedList is LinkedList and the underlying data structure for ArrayDeque is a growable Array. @iSergius

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment