3

I understand that the intricacies of Java generics can get... intricate. I figure that if I have a Google Guava multimap with a Collection<? extends FooBar>, I should be able to use getMap() to assign that to a Map<String, ? extends FooBar>. But I can't do that in Eclipse 4.4RC2; it tells me, "Type mismatch: cannot convert from Map<String,Collection<capture#1-of ? extends EclipseGenerics.FooBar>> to Map<String,Collection<? extends EclipseGenerics.FooBar>>".

import java.util.*;

import com.google.common.collect.*;

public class EclipseGenerics {

  public static interface FooBar {
    public String getFoo();
  }

  public static <FB extends FooBar> SetMultimap<String, FB> groupFooBarByBar(final Iterable<FB> foobars) {
    final SetMultimap<String, FB> groupedFoobars = HashMultimap.create();
    for (final FB foobar : foobars) {
      groupedFoobars.put(foobar.getFoo(), foobar);
    }
    return groupedFoobars;
  }

  public static void test() {
    Collection<FooBar> foobars = new ArrayList<>();
    final SetMultimap<String, ? extends FooBar> groupedFooBars = groupFooBarByBar(foobars);
    //why can't I do this?
    final Map<String, Collection<? extends FooBar>> map = groupedFooBars.asMap();
  }

}

I cannot do this:

final Map<String, Collection<? extends FooBar>> map = groupedFooBars.asMap();

Why not? asMap() should return Map<K,Collection<V>>.

But I can do this!

final Map<String, ? extends Collection<? extends FooBar>> map = groupedFooBars.asMap();

I get a similar error with Oracle's Java 1.8 javac compiler, so either Eclipse 4.4RC2 and Oracle are both wrong, or (more likely) I'm still not a complete expert on Java generics.

Anyone care to explain why Map<String, Collection<? extends FooBar>> doesn't work, but Map<String, ? extends Collection<? extends FooBar>> does work?

ErikE
  • 48,881
  • 23
  • 151
  • 196
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • I retract my previous comment ... Now that I see what you're doing, I agree.. That's wrong. – Christopher Wirt May 29 '14 at 17:44
  • What type does `asMap()` return? Based on your final two lines of code, I think Eclipse is right... I have to admit though that the error that Eclipse is making doesn't really make a whole lot of sense to me -- not really sure where that `capture#1` came from... – awksp May 29 '14 at 17:46
  • `asMap()` returns `Map>`. – Garret Wilson May 29 '14 at 17:49
  • 1
    What happens if you try `javac`? – awksp May 29 '14 at 17:59
  • hehe I was just getting to that. I'm doing a Maven-based build now and... Oracle's Java 1.8.0 javac says the same thing. I still don't understand why, but at least Oracle and Eclipse are in agreement here. – Garret Wilson May 29 '14 at 18:13
  • Hmmm, if `javac` and Eclipse agree it's likely that this is proper behavior... Unfortunately, I'm just as confuzzled as you are as to why this doesn't work. Is the returned error message the same? – awksp May 29 '14 at 18:30
  • Pretty much the same error message, yes. – Garret Wilson May 29 '14 at 18:31
  • 3
    See also http://stackoverflow.com/questions/18907262/bounded-wildcard-related-compiler-error. – pingw33n May 29 '14 at 19:14
  • @unholysampler, you seem to be correct that this is a duplicate, but not for the post you provided. pingw33n provided the correct post. Please edit your duplicate flag to indicate the correct post. – Garret Wilson May 29 '14 at 19:46
  • I think we need a Moderator to do that. Otherwise we have to reopen the question and close it again. – JasonMArcher May 30 '14 at 18:52

0 Answers0