Multimap from map

750 views
Skip to first unread message

Stanislav Kurilin

unread,
Nov 19, 2012, 6:56:19 AM11/19/12
to guava-...@googlegroups.com
We can create multimaps from other multimaps using factory methods. We can view multimaps as maps. But we can not create multimap from maps using factory methods. Why?

Louis Wasserman

unread,
Nov 19, 2012, 8:41:14 AM11/19/12
to Stanislav Kurilin, guava-...@googlegroups.com
Well, there's always Multimaps.newXXXMultimap, though that requires that the map be empty; there's also Multimaps.forMap, which views a Map<K, V> as a Multimap<K, V>

But what you're probably asking is about Map<K, Collection<V>>.

The relevant feature request is Guava issue 465, and possibly 1165.

IIRC, the reason we haven't necessarily decided to accept those issues yet is that we're having trouble coming up with use cases where the "Right Thing to Do" is to convert a Map<K, Collection<V>> to a Multimap<K, V>, instead of using a Multimap from the beginning and never bothering with a Map<K, Collection<V>> at all.

Of course, there's also the perfectly good workaround of

for (Map.Entry<K, Collection<V>> entry : map.entrySet()) {
  multimap.putAll(entry.getKey(), entry.getValue());
}

On Mon, Nov 19, 2012 at 6:56 AM, Stanislav Kurilin <st.ku...@gmail.com> wrote:
We can create multimaps from other multimaps using factory methods. We can view multimaps as maps. But we can not create multimap from maps using factory methods. Why?



--
Louis Wasserman

Stanislav Kurilin

unread,
Nov 19, 2012, 10:01:23 AM11/19/12
to guava-...@googlegroups.com, Stanislav Kurilin
In my case I get Map<K, Collection<V>> throw `transformEntries` of Map<K, V0>. Is it wrong think to do or just rare case?

Louis Wasserman

unread,
Nov 19, 2012, 10:03:05 AM11/19/12
to Stanislav Kurilin, guava-...@googlegroups.com
That's perfectly fine; it's just relatively rare.  That said, can you explain more about what exactly you're doing with that map and why you chose this approach, so we can study that as an example use case?

Stanislav Kurilin

unread,
Nov 19, 2012, 11:05:43 AM11/19/12
to guava-...@googlegroups.com, Stanislav Kurilin
I have some collection of objects O. I can retrieve some V from each O.  I should keep track of V per each O. So I build Multimap<O, V> before and after changes and then analyze difference.
Reply all
Reply to author
Forward
0 new messages