Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with/withoutmethods on immutable collections #306

Closed
GoogleCodeExporter opened this issue Apr 7, 2015 · 2 comments
Closed

with/withoutmethods on immutable collections #306

GoogleCodeExporter opened this issue Apr 7, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link


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)

Original issue reported on code.google.com by jvdne...@gmail.com on 9 Dec 2009 at 2:29

@GoogleCodeExporter
Copy link
Author


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

public static <K,V> ImmutableMap<K,V> without(final ImmutableMap<K,V> map, 
        final K key) {
    final Map<K, V> filtered = Maps.filterKeys(map, 
            Predicates.not(Predicates.in(Collections.singleton(key))));
    return ImmutableMap.copyOf(filtered);
}

public static <K,V> ImmutableMap<K,V> without(final ImmutableMap<K,V> map, 
        final Set<K> keys) {
    final Map<K, V> filtered = Maps.filterKeys(map, 
            Predicates.not(Predicates.in(keys)));
    return ImmutableMap.copyOf(filtered);
}

Original comment by jvdne...@gmail.com on 16 Dec 2009 at 1:26

@GoogleCodeExporter
Copy link
Author

This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09

  • Changed state: Moved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant