Skip to content

Strict version of Map.filterKeys #176

Description

@OndrejSpanel

Map.filterKeys is deprecated as the moment. Its deprecation message says:

Use .view.filterKeys(f). A future version will include a strict version of this method (for now, .view.filterKeys(p).toMap).

The suggested .view.filterKeys(p).toMap is not very efficient, it rebuilds the whole map. Could a strict version be added as promised by the deprecation warning?

Following workaround provides a more efficient solution for those interested:

  implicit class MoreMapOps[K, V](private val m: Map[K, V]) extends AnyVal {
    def filterKeysStrict(predicate: K => Boolean): Map[K, V] = {
      m.removedAll(m.keys.filterNot(predicate))
    }
  }

See also https://stackoverflow.com/q/78354572/16673

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions