Skip to content

with/withoutmethods on immutable collections #306

@gissuebot

Description

@gissuebot

Original issue created by jvdneste on 2009-12-09 at 02:29 PM


Something along the lines of:

final ImmutableMap<Integer,String> map = ImmutableMap.of(1, "a", 2, "b");

ImmutableMap<Integer,String> result;
result = map.with(3, "c"); // { 1 : "a", 2 : "b", 3 : "c" }
result = map.with(3, "foo"); // { 1 : "a", 2 : "b", 3 : "foo" }
result = map.without(3); // { 1 : "a", 2 : "b" }

and possibly bulk overloads

I understand the performance implications of using these, but they would
still be very useful. Currently I use:

public static <K,V> ImmutableMap<K,V> with(
  final ImmutableMap<K,V> map, final K key, final V value) {
    return ImmutableMap.<K,V>builder()
      .putAll(Maps.filterKeys(map,
        Predicates.not(Preds.is(key)))).put(key, value).build();
}

(Preds.is is what used to be Predicates.isSameAs)

But I would think that this can be done better if it were implemented in
the collection.

In the long run, immutable collections would need a different
implementation to be able to use these methods in moderately performance
sensitive areas. (the way clojure's persistent data structures are
implemented comes to mind, and I would think scala has something similar,
though I am not sure)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions