LostKe / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Immutable builder remove() method #279

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The builder classes which create instances of Immutable{Map,List,etc} 
should have a remove method, this would simplify the process of creating 
(for example) a map containing one less entry than an existing map.

The method:
ImmutableMap remove(ImmutableMap map, Object key) {
  HashMap m = new HashMap(map);
  m.remove(key);
  return ImmutableMap.copyOf(m);
}

could become:
ImmutableMap remove(ImmutableMap map, Object key) {
  return ImmutableMap.builder().putAll(map).remove(key).build();
}

Original issue reported on code.google.com by hartford...@gmail.com on 27 Oct 2009 at 10:12

GoogleCodeExporter commented 8 years ago
Our attitude toward this so far has been that it crosses the line toward 
overloading 
the builders with too much functionality. We don't want the builders to become 
poor-
man's collections. I think it's reasonable to have to do what you're doing 
currently 
... or the admittedly odd 

  ImmutableMap.copyOf(Maps.filterKeys(
      map, Predicates.not(Predicates.is(key))))

I'm not closing this yet, though.

Original comment by kevin...@gmail.com on 27 Oct 2009 at 10:22

GoogleCodeExporter commented 8 years ago

Original comment by kevin...@gmail.com on 5 Nov 2009 at 1:07

GoogleCodeExporter commented 8 years ago
OK, I have a use case for this. I have a CopyOnWriteMap class that internally 
keeps an effectively immutable 
(volatile) map reference. When performing a mutating operation it takes a lock, 
then copies the map, modifies it, 
writes back the modified reference and then releases the lock. This works great 
for very fast read-mostly 
modestly sized caches.

I'd like to be able to use ImmutableMap (and ImmutableSortedMap) as the 
internal storage for these, but without 
the Builder supporting mutating methods I need two copies per mutator to 
achieve this.

Original comment by jed.wesl...@gmail.com on 18 Nov 2009 at 3:44