Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava: merge two multimaps

Tags:

java

guava

I have tree classes e.g. ClassA, ClassB, ClassC. ClassA and ClassB extends ClassC. I have two Multimaps - Multimap<Integer, ClassA> and Multimap<Integer, ClassB> and I would like to merge this two multimaps into one. I have tried find some solution but unsuccessfully. I have tried sth. like Multimap<Integer, ? extends ClassC> but I don't know if I make it right and if I can merge two multimaps together. Can someone help me? Thank you for response, I appreciate every help.

like image 320
Martin Avatar asked Feb 18 '13 15:02

Martin


2 Answers

Multimap<Integer, ? extends ClassC> would mean that the generic type can be any type that extends ClassC, but the type has to be fixed. i.e, it can be either all ClassA or all ClassB. So you should use Multimap<Integer, ClassC> instead. It will accept both the types ClassA and ClassB.

like image 87
Hari Menon Avatar answered Nov 11 '22 17:11

Hari Menon


Multimap<Integer, ClassC> combine(Multimap<Integer, ? extends ClassC> a, Multimap<Integer, ? extends ClassC> b) {
  Multimap<Integer, ClassC> combined = new SetMultimap<Integer, ClassC>();  // or whatever kind you'd like
  combined.putAll(a);
  combined.putAll(b);
  return combined;
}
like image 31
jacobm Avatar answered Nov 11 '22 19:11

jacobm