Issue 306 in google-collections: with/withoutmethods on immutable collections

0 views
Skip to first unread message

codesite...@google.com

unread,
Dec 9, 2009, 9:30:39 AM12/9/09
to google-coll...@googlegroups.com
Status: New
Owner: ----

New issue 306 by jvdneste: with/withoutmethods on immutable collections
http://code.google.com/p/google-collections/issues/detail?id=306


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)


--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

codesite...@google.com

unread,
Dec 16, 2009, 8:27:48 AM12/16/09
to google-coll...@googlegroups.com

Comment #1 on issue 306 by jvdneste: with/withoutmethods on immutable
collections
http://code.google.com/p/google-collections/issues/detail?id=306


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);
Reply all
Reply to author
Forward
0 new messages