9

I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for each key. See the example:

Multimap<Object, Object> mmap = Multimaps.newMultimap(
    Maps.<Object, Collection<Object>>newHashMap(), 
    new Supplier<Collection<Object>>() {
  public Collection<Object> get() {
    return HashMultiset.create();
  }
});
mmap.put("1", "2");

But then,

System.out.println(mmap.get("1") instanceof Multiset<?>); 
//false, the returned collection is not a HashMultiset,
//but a (private) WrappedCollection

So it seems I cannot access the multiset I created? I wanted to be able to return that, as a Multiset (wrapped in Multisets.unmodifiableMultiset()). I don't want to copy it into a new Multiset each time either. Do I have any other option than switching back to Map<K, Multiset<V>> and adding in my code the complexity that Multimap meant to eliminate?

Dimitris Andreou
  • 8,806
  • 2
  • 33
  • 36

2 Answers2

6

I'm afraid this doesn't seem to be possible. You should file a feature request. I have a sneaking suspicion those crafty Google folks have a nifty kind of a Multimap that they could potentially release that might potentially help you.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
0

The Multimap interface only specifies that get(K) return a Collection<V>. I think it would be poor design for your code to depend or assume anything else.

Can you expand a little bit more on what you are trying to do here? Seems like there should be an easier way to accomplish it.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • 4
    The short version of the question is: "I just want a multimap where I can see the values through the Multiset API, not the Collection API". – Dimitris Andreou Jan 22 '10 at 18:55