jamesbrowder / guava-libraries

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

Multimaps.filterKeys #737

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I recently had to do the following: take a Multimap and filter the keys to 
produce another copy.

I did the following:
Multimap<K, V> input;
Map<K, Collection<V>> filteredMap = Maps.filterKeys(input.asMap(), p);
Multimap<K, V> newMap = Multimaps.newListMultimap(filteredMap, s);

This gave an IllegalArgumentException as the passed map may not be null (see 
Issue ).

So I had to do the following:

Multimap<K, V> newMap = ArrayListMultimap.create();
for (K key : filteredMap.keySet()){
   newMap.putAll(key, filteredmap.get(key));
}

Since not all functions in Maps are implemented in Multimaps you have cases 
where a Multimap will be made a Map<K, Collection<V>>. There should be an easy 
way to transform / copy the Map<K, Collection<V>> back to a Multimap.

Multimaps.copy(Map<K, Collection<V>>, Supplier<? extends List<V>> sup);

Original issue reported on code.google.com by dancerj...@gmail.com on 4 Oct 2011 at 6:36

GoogleCodeExporter commented 9 years ago
See related Issue 736:
http://code.google.com/p/guava-libraries/issues/detail?id=736&colspec=ID%20Type%
20Status%20Milestone%20Summary&start=100

Original comment by dancerj...@gmail.com on 4 Oct 2011 at 6:37

GoogleCodeExporter commented 9 years ago
Out of curiosity, would you be satisfied by something in ImmutableMap.Builder?

Original comment by wasserman.louis on 10 Oct 2011 at 2:39

GoogleCodeExporter commented 9 years ago
First, I assume you mean ImmutableMultimap.Builder since I want to create a 
Multimap from a Map of Collections. 

But if I need a mutable Multimap, how would I get it from the created immutable 
Multimap? There is not a mechanism in Multimaps to do a copy or am I missing 
something?

Original comment by dancerj...@gmail.com on 10 Oct 2011 at 2:51

GoogleCodeExporter commented 9 years ago
HashMultimap.create(Multimap) is a "copy constructor" style factory.

Original comment by wasserman.louis on 20 Oct 2011 at 7:23

GoogleCodeExporter commented 9 years ago
Yes, but it does not take a Map<K, Collection<V>> as is returned by 
Multimap.asMap(). The point I am trying to make is that to utilize Maps 
functionality on a Multimap you must use asMap() but then you can't get it back 
into a Multimap form easily.

Original comment by dancerj...@gmail.com on 20 Oct 2011 at 8:26

GoogleCodeExporter commented 9 years ago
I was answering your second question:

> But if I need a mutable Multimap, how would I get it from the created 
immutable
> Multimap? There is not a mechanism in Multimaps to do a copy or am I missing
> something?

Original comment by wasserman.louis on 20 Oct 2011 at 9:24

GoogleCodeExporter commented 9 years ago
Oh. Gotcha. Just seems like a lot of processing. If I need to do "Maps" 
processing on my Multimap, I need to do as asMap, the processing, then create 
an ImmutableMultimap and then copy that to create a mutable Multimap. Seems 
like there should be a better way. Unless one or the other of the 
ImmutableMulitmap.Builder / Multimap.create don't iterate, the Map is being 
iterated twice to turn it back into a Multimap.

All that said, as least with an ImmutableMulitmap.Builder that takes a Map<K, 
Collection<V>> there is a way to do it.

Original comment by dancerj...@gmail.com on 20 Oct 2011 at 9:33

GoogleCodeExporter commented 9 years ago
Let's step back to your actual use case:

Would you be satisfied by Multimaps.filterKeys() ?

Original comment by wasserman.louis on 20 Oct 2011 at 9:54

GoogleCodeExporter commented 9 years ago
Yes. Having the various Maps... functionality for Multimaps is what I am 
looking for. So in this case, yes a Multimaps.fitlerKeys is what I needed.

Original comment by dancerj...@gmail.com on 20 Oct 2011 at 9:58

GoogleCodeExporter commented 9 years ago
So let's deal with that use case instead of going all the way around like this. 
 ;)

Original comment by wasserman.louis on 20 Oct 2011 at 10:05

GoogleCodeExporter commented 9 years ago
Sounds good to me.

Original comment by dancerj...@gmail.com on 20 Oct 2011 at 10:07

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Since the update this is now a duplicate of Issue #625

Original comment by fin...@gmail.com on 21 Oct 2011 at 12:50

GoogleCodeExporter commented 9 years ago
Concur.

Original comment by dancerj...@gmail.com on 21 Oct 2011 at 8:41

GoogleCodeExporter commented 9 years ago
Marked as such.

Original comment by wasserman.louis on 29 Nov 2011 at 4:12

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09