Class TreeMap<K, V, R>

An ordered Map backed by a red-black tree.

  • Iteration order is ascending by key.
  • No node exposure: all APIs use keys/values only.

Type Parameters

  • K = any
  • V = any
  • R = [K, V]

Implements

  • Iterable<[K, V | undefined]>

Constructors

  • Create a TreeMap from an iterable of [key, value] entries or raw elements.

    Type Parameters

    • K = any
    • V = any
    • R = [K, V]

    Parameters

    • entries: Iterable<[K, undefined | V], any, any> | Iterable<R, any, any> = []

      Iterable of [key, value] tuples, or raw elements if toEntryFn is provided.

    • options: TreeMapOptions<K, V, R> = {}

      Configuration options including optional toEntryFn to transform raw elements.

    Returns TreeMap<K, V, R>

    If any entry is not a 2-tuple-like value (when no toEntryFn), or when using the default comparator and encountering unsupported/invalid keys (e.g. NaN, invalid Date).

    // Standard usage with entries
    const map = new TreeMap([['a', 1], ['b', 2]]);

    // Using toEntryFn to transform raw objects
    const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
    const map = new TreeMap<number, User, User>(users, { toEntryFn: u => [u.id, u] });

Accessors

Methods

  • Iterate over [key, value] entries in ascending key order.

    Note: values may be undefined.

    Returns IterableIterator<[K, undefined | V], any, any>

  • Visit each entry in ascending key order.

    Note: callback value may be undefined.

    Parameters

    • cb: ((value: undefined | V, key: K, map: TreeMap<K, V, [K, V]>) => void)
        • (value, key, map): void
        • Parameters

          Returns void

    • OptionalthisArg: any

    Returns void

  • Return all entries in a given key range.

    Parameters

    • range: [K, K]

      [low, high]

    • options: TreeMapRangeOptions = {}

      Inclusive/exclusive bounds (defaults to inclusive).

    Returns [K, undefined | V][]

  • Iterate over values in ascending key order.

    Note: values may be undefined (TreeMap allows storing undefined, like native Map).

    Returns IterableIterator<undefined | V, any, any>

  • Create the strict default comparator.

    Supports:

    • number (rejects NaN; treats -0 and 0 as equal)
    • string
    • Date (orders by getTime(), rejects invalid dates)

    For other key types, a custom comparator must be provided.

    Type Parameters

    • K

    Returns Comparator<K>