javascript-collections-framework
    Preparing search index...

    Function newSetFromMap

    • Returns a set backed by map: its members are the map's keys, iterated, compared, hashed and printed as the map orders and compares its keys.

      The set is live in both directions. add(member) calls map.getOrInsert(member, true), so the map's key rules, access order and maxSize eviction apply; a present member is kept and its value is not written. addAll returns true when some element was absent (map.has) just before its own add, so it calls the map twice per element. Iteration, cursor, forEach and every removal go through map.keySet(), so the set iterates live or fails fast as that key set does. Changes made directly to the map show through the set, and the set's size and revision are the map's. Writing a value of a present key leaves membership unchanged.

      Errors of the set's own argument checks name SetFromMap.<method>; errors raised by the map keep the map's name, and iteration errors keep the key set's name.

      Time: O(1), or the map's isEmpty cost for a range view of a sorted map (O(log n)); each operation of the returned set costs what the matching map or key set operation costs.

      Type Parameters

      • E

      Parameters

      • map: MapCollection<E, boolean>

        An empty map from this library (any copy of the same major version), or an empty range or reversed view of one.

      Returns SetCollection<E>

      A set whose members are the map's keys.

      TypeError when map is not a map from this library.

      IllegalArgumentError when map is not empty.

      // A set that keeps the 100 most recently added members.
      const recent = newSetFromMap(
      new LinkedHashMap<string, boolean>({ accessOrder: true, maxSize: 100 }),
      )
      recent.add('a').add('b').add('a') // re-adding 'a' makes it the most recent member
      recent.toArray() // ['b', 'a']