Built-in collections

20 collections and 88 operations across C#, Python, Java and C++: what each operation does, what it costs and why, what it does to memory, and a tested example. Every example on these pages is run by the Lab's test suite.

The same idea in four languages

Not a perfect match everywhere: .NET has no standard double-ended queue, Python's heapq is a set of functions on a list, and std::priority_queue is an adapter that defaults to a max-heap. Each page explains its differences.

Find a collection or operation

  • C# · Sequences

    List<T>

    A growable array: contiguous storage with spare capacity, O(1) indexing and amortized O(1) appends at the end.

  • C# · Maps

    Dictionary<TKey, TValue>

    A hash table from unique keys to values with expected O(1) insert, lookup and removal.

  • C# · Sets

    HashSet<T>

    A hash table of unique elements with expected O(1) add, membership test and removal, plus in-place set algebra.

  • C# · Queues and deques

    Queue<T>

    A first-in, first-out queue on a circular array: O(1) dequeue and amortized O(1) enqueue.

  • C# · Priority queues

    PriorityQueue<TElement, TPriority>

    A min-priority queue: elements with the smallest priority come out first; O(log n) enqueue and dequeue.

  • Python · Sequences

    list

    Python's built-in growable array of object references: O(1) indexing and amortized O(1) append at the end.

  • Python · Maps

    dict

    Python's built-in hash table from hashable keys to values, with average O(1) operations and guaranteed insertion order.

  • Python · Sets

    set

    Python's built-in hash set of unique hashable elements with average O(1) membership and operators for set algebra.

  • Python · Queues and deques

    collections.deque

    A double-ended queue with O(1) appends and pops at both ends; the standard queue and stack in Python.

  • Python · Priority queues

    heapq

    Functions that keep a plain list in min-heap order: O(log n) push and pop, O(1) peek at heap[0].

  • Java · Sequences

    ArrayList<E>

    Java's growable array: O(1) get/set, amortized constant-time add at the end, linear-time insertion and removal elsewhere.

  • Java · Maps

    HashMap<K, V>

    Java's hash table map: constant-time get and put on average, one null key allowed, no ordering.

  • Java · Sets

    HashSet<E>

    A set backed by a HashMap: constant-time add, contains and remove on average; one null allowed.

  • Java · Queues and deques

    ArrayDeque<E>

    A resizable circular array usable as a queue or a stack: amortized constant time at both ends, no nulls.

  • Java · Priority queues

    PriorityQueue<E>

    An unbounded binary min-heap: O(log n) offer and poll, O(1) peek, linear remove(Object) and contains.

  • C++ · Sequences

    std::vector<T>

    The standard contiguous dynamic array: elements stored inline, O(1) indexing, amortized constant-time push_back.

  • C++ · Maps

    std::unordered_map<Key, T>

    The standard hash map: average constant-time insert, find and erase, worst case linear.

  • C++ · Sets

    std::unordered_set<Key>

    The standard hash set: average constant-time insert, membership and erase.

  • C++ · Queues and deques

    std::deque<T>

    A double-ended queue with constant-time insertion at both ends and constant-time random access; the default storage of std::queue and std::stack.

  • C++ · Priority queues

    std::priority_queue<T>

    A container adapter that keeps a vector in heap order with std::push_heap/pop_heap: O(log n) push and pop, O(1) top. A max-heap by default.