Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Runtime Complexity of Java Collections
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
@shrishar
Copy link

shrishar commented Aug 30, 2017

Amazing thank you so much ~!

Loading

@bitnahian
Copy link

bitnahian commented Sep 21, 2017

Thank you for this great chart!

Loading

@rafareyes7
Copy link

rafareyes7 commented Sep 27, 2017

Thanks! source?

Loading

@SurendraVidiyala
Copy link

SurendraVidiyala commented Oct 5, 2017

Thank you

Loading

@firstpixel
Copy link

firstpixel commented Nov 24, 2017

Awesome! Great reference, but its missing Stack.

Loading

@pendawleabhay
Copy link

pendawleabhay commented Nov 29, 2017

So LinkedList under List Interface uses Linked List data structure. But LinkedList under Queue interface uses Array? How??

Loading

@ayush--s
Copy link

ayush--s commented Dec 31, 2017

Loading

@vlytsus
Copy link

vlytsus commented Jan 30, 2018

ArrayDequeue spelled wrong. Should be ArrayDeque. And Data Structure for ArrayDeque is array.

Loading

@ArseniyChern
Copy link

ArseniyChern commented Feb 3, 2018

HashMap should be log(n) if worst case

Loading

@baruchiro
Copy link

baruchiro commented Mar 27, 2018

Thanks!
What about constructors?
They are not always as added.

Loading

@pszeliga
Copy link

pszeliga commented May 16, 2018

I think you switched data structures for LinkedList and ArrayDeque

Loading

@matthewmarkose
Copy link

matthewmarkose commented May 29, 2018

This is beautiful. Good job and thank you.

Loading

@abcoep
Copy link

abcoep commented Jun 3, 2018

I think you switched data structures for LinkedList and ArrayDeque

@pszeliga I second you

Loading

@slavadolg
Copy link

slavadolg commented Jun 25, 2018

In Queue section LinkedList isnt an Array and ArrayDequeue based on cycled array

Loading

@prajeethrudra
Copy link

prajeethrudra commented Aug 1, 2018

Thank You

Loading

@Bhagyasree1234
Copy link

Bhagyasree1234 commented Aug 19, 2018

In case of O(h / n) may i know the 'h' value and what does it stands for.

Loading

@lare96
Copy link

lare96 commented Oct 16, 2018

LinkedList remove is only O(1) if you use its iterator. standard remove(Object) is O(n)

Loading

@digi9ten
Copy link

digi9ten commented Oct 25, 2018

In case of O(h / n) may i know the 'h' value and what does it stands for.

h stands for the current capacity of the hash collection.

Loading

@dmitry-izmerov
Copy link

dmitry-izmerov commented Dec 3, 2018

@psayre23 could you explain or anybody why there is no put/add operation for map in table above?

Loading

@enkeebbx
Copy link

enkeebbx commented Mar 11, 2019

Simply amazing. Thank you.

Loading

@ZhijianD
Copy link

ZhijianD commented Apr 5, 2019

It really helps me to prepare for my CS exam! Thank you!!

Loading

@chinmay2312
Copy link

chinmay2312 commented Apr 7, 2019

For Queue, the 2nd column head would be "peek" instead of "peak"

Loading

@dongw00
Copy link

dongw00 commented Apr 14, 2019

Thank you bro

Loading

@cmulation
Copy link

cmulation commented Jun 18, 2019

Very useful! Thanks!

Loading

@agrawalankit006
Copy link

agrawalankit006 commented Jul 11, 2019

Thankyou! Only a small mistake that I think is LinkedList remove is O(N) not O(1) because it first needs to find the node before deleting it.

Loading

@HUAZHEYINy
Copy link

HUAZHEYINy commented Aug 5, 2019

thanks !!!

Loading

@LisaFan18
Copy link

LisaFan18 commented Sep 14, 2019

Very useful. Thank you!

Loading

@VasilSokolov
Copy link

VasilSokolov commented Nov 1, 2019

Great!!! Thanks!

Loading

@MariaForester
Copy link

MariaForester commented Nov 17, 2019

Thanks a lot! Very helpful!

Loading

@zeiadzaf
Copy link

zeiadzaf commented Dec 21, 2019

This is great! Thanks
you might need to swap datastructure of ArrayDeque and LinkedList under Deque

Loading

@lucallero
Copy link

lucallero commented Feb 23, 2020

Awesome! Great reference, but its missing Stack.

Very helpful indeed.
Please, add the stack as @firstpixel mentioned.
Thanks.

Loading

@o-x-y-g-e-n
Copy link

o-x-y-g-e-n commented Mar 9, 2020

Thank you for this!

Loading

@Hydor
Copy link

Hydor commented Apr 1, 2020

Wow! Thank you! That's very helpful!

Loading

@priyodas12
Copy link

priyodas12 commented Apr 26, 2020

How O(1) for adding in arraylist?

Loading

@ssavva05
Copy link

ssavva05 commented May 24, 2020

Thanks, Great Resource! I have to build my Programs in a way that saves time!

Loading

@shyamzzp
Copy link

shyamzzp commented Aug 10, 2020

Thankyou! Only a small mistake that I think is LinkedList remove is O(N) not O(1) because it first needs to find the node before deleting it.

Correct

Loading

@ashtnemi448
Copy link

ashtnemi448 commented Sep 23, 2020

To the point. Thanks man!

Loading

@Barry36
Copy link

Barry36 commented Nov 2, 2020

just curious how about the complexity of ArrayList.addAll(Collection)? is it Constant time?

Loading

@mcanalesmayo
Copy link

mcanalesmayo commented Nov 11, 2020

just curious how about the complexity of ArrayList.addAll(Collection)? is it Constant time?

@Barry36 nope, it's O(M+N) where M = array size (the ArrayList) and N = collection size (the function argument Collection).

FYI, the source code of ArrayList.addAll in JDK 11:

    /**
     * Appends all of the elements in the specified collection to the end of
     * this list, in the order that they are returned by the
     * specified collection's Iterator.  The behavior of this operation is
     * undefined if the specified collection is modified while the operation
     * is in progress.  (This implies that the behavior of this call is
     * undefined if the specified collection is this list, and this
     * list is nonempty.)
     *
     * @param c collection containing elements to be added to this list
     * @return {@code true} if this list changed as a result of the call
     * @throws NullPointerException if the specified collection is null
     */
    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        modCount++;
        int numNew = a.length;
        if (numNew == 0)
            return false;
        Object[] elementData;
        final int s;
        if (numNew > (elementData = this.elementData).length - (s = size))
            elementData = grow(s + numNew);
        System.arraycopy(a, 0, elementData, s, numNew);
        size = s + numNew;
        return true;
    }
  1. In the very first line of code, it converts collection to array. If the collections is for example a LinkedList, this means it needs to iterate over all the elements and insert it into a new array, so this is already O(N).
  2. In the worst case scenario, the array (of the ArrayList) doesn't have enough capacity to "accommodate" the new elements to be added, so it needs to create a copy of the current elements into a new bigger array (O(M)).
  3. Finally, it uses System.arraycopy to copy the array of new elements (of the Collection) into the grown array (of the ArrayList). I didn't see the source code of the System.arraycopy function, but the easiest and most intuitive way of implementing an array copy is iterating over all elements, which makes it O(N) again. But even if the implementation of this had better time complexity, the overall time complexity of the addAll function would not change. Imagine System.arraycopy is O(1), the complexity of the whole function would still be O(M+N). And if the complexity of the System.arraycopy was O(N), overall complexity would still be O(M+N).

Loading

@kumaresan-perumal
Copy link

kumaresan-perumal commented Mar 24, 2021

good thanks

Loading

@kumaresan-perumal
Copy link

kumaresan-perumal commented Apr 12, 2021

Hi, can you please add Vector?

Loading

@davitescobedo
Copy link

davitescobedo commented May 3, 2021

How O(1) for adding in arraylist?

Being a list, you always add at the end and having an array as the underlying data structure access is O(1).

If you are asking about having to grow the array and the time in reallocating that memory and copying it, it is done through amortized complexity: each time we add an element that goes beyond the total size of the array, the capacity is doubled. So that way, most of the times you add a new element you just add at the end with O(1) and doing an average, the runtime is constant https://stackoverflow.com/a/45243529/15001063

Loading

@ahmedghallab
Copy link

ahmedghallab commented Jun 5, 2021

LinkedList remove is only O(1) if you use its iterator. standard remove(Object) is O(n)

Thankyou! Only a small mistake that I think is LinkedList remove is O(N) not O(1) because it first needs to find the node before deleting it.

Yes.

Check out this link:
https://stackoverflow.com/questions/7294634/what-are-the-time-complexities-of-various-data-structures

Loading

@zhengyin
Copy link

zhengyin commented Nov 12, 2021

thanks !!!

Loading

@oziris78
Copy link

oziris78 commented Nov 17, 2021

best gist ever!

Loading

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