Why this simple java generic function won't compile?

Up vote 5 down vote favorite share g+ share fb share tw.

While f1 does compile, the very similar f2 won't I and just cant explain why. (Tested on Intellij 9 and Eclipse 3.6) And really I thought I was done with that kind of question. Import java.util.

*; public class Demo { public List> f1(){ final List> list = null; return list; } public List>> f2(){ final List>> list = null; return list; } } java generics link|improve this question asked Sep 5 '10 at 1:45lacroix154738628 75% accept rate.

– linuxuser27 Sep 5 '10 at 1:47 incompatible types found : java.util. List>> required: java.util. List>> – lacroix1547 Sep 5 '10 at 1:54 2 Why don't you just use List>> at all places?

That's also the normal approach. You'd like to declare the interfaces, not implementations. – BalusC Sep 5 '10 at 1:56 Was trying to add an com.google.common.collect.

ImmutableSet in that list. – lacroix1547 Sep 5 '10 at 2:05.

List>> is not assignable to List>> for the same reason List> would not be assignable to List>. You can get it to compile by changing this: public List>> f2(){ into this: public List>> f2(){ The reason your code didn't compile, and why the other example I gave (ie: "List> would not be assignable to List>") is that Java generics are not covariant. The canonical example is that even if Circle extends Shape, List does not extend List.

If it did, then List would need to have an add(Shape) method that accepts Square objects, but obviously you don't want to be able to add Square objects to a List. When you use a wildcard, you're getting a type that slices away certain methods. List retains the methods that return E, but it doesn't have any of the methods that take E as a parameter.

This means you still have the E get(int) method, but add(E) is gone. List is a super-type of List as well as List, List, etc. (? super wildcards slice the other way: methods that return values of the type parameter are removed) Your example is more complicated because it has nested type parameters, but it boils down to the same thing: List> is a sub-type of List> Because generics are not covariant, wrapping the two types in a generic type (like List) yields a pair of types that no longer have the sub/super-type relationship. That is, List>> is not a sub-type of List>> If instead of wrapping with List you wrap with List you'll end up with the original relationship being preserved.

(This is just a rule of thumb, but it probably covers 80% of the cases where you'd want to use wildcards. ) Note that trashgod and BalusC are both correct in that you probably don't want to be returning such a weird type. List>> would be a more normal return type to use.

That should work fine as long as you're consistent about always using the collection interfaces rather than the concrete collection classes as type parameters. Eg: you can't assign a List> to a List>, but you can put ImmutableSet instances into a List>, so never say List>, say List>.

– lacroix1547 Sep 5 '10 at 1:59 "for the same reason List> would not be assignable to List> " As lacroix1547 noted, they are assignable: in method f1. – Nikita Rybak Sep 5 '10 at 2:03 Laurence your fix works but why? – lacroix1547 Sep 5 '10 at 2:18 Did you just randomly tried different solutions?

– lacroix1547 Sep 5 '10 at 2:19 @Nikita: yeah, that was a typo. I've corrected it now. – Laurence Gonsalves Sep 5 '10 at 2:29.

Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code. "—Joshua Bloch, Effective Java Second Edition, Chapter 5, Item 28.

This is just a deep private method, not part of any api. – lacroix1547 Sep 5 '10 at 2:28 2 That's reasonable. I didn't mean to seem censorious; I just wanted to adduce the principle.

– trashgod Sep 5 '10 at 2:39 1 +1, a very important principle every Java programmer must know (and follow). – missingfaktor Sep 5 '10 at 5:15.

This is too long to fit in a comment. I just wanted to say that it makes no sense to declare it that way. You can also just do the following: public List>> f2() { List>> list = new ArrayList>>(); List> nestedList = new ArrayList>(); list.

Add(nestedList); Set set = new HashSet(); nestedList. Add(set); return list; } Works as good. I see no point of using?

Extends SomeInterface here. Update: as per the comments, you initially wanted to solve the following problem: public List>> getOutcomes() { Map, Integer> map = new HashMap, Integer>(); List>> outcomes = new ArrayList>>(); for (Map. Entry, Integer> entry : map.entrySet()) { outcomes.

Add(asMap(entry.getValue(), entry.getKey())); // add() gives compiler error: The method add(Map>) // in the type List>> is not applicable for // the arguments (Map>) } return outcomes; } public Map asMap(K k, V v) { Map result = new HashMap(); result. Put(k, v); return result; } This can just be solved by declaring the interface (Set in this case) instead of implementation (HashSet in this case) as generic type. So: public List>> getOutcomes() { Map, Integer> map = new HashMap, Integer>(); List>> outcomes = new ArrayList>>(); for (Map.

Entry, Integer> entry : map.entrySet()) { outcomes. Add(asMap(entry.getValue(), entry.getKey())); // add() now compiles fine. } return outcomes; } In future problems, try to ask how to solve a particular problem, not how to achieve a particular solution (which in turn may not be the right solution after all).

Yea I know but it solves import java.util. *; public class Demo2 { public List>> getOutcomes() { Map, Integer> map = new HashMap, Integer>(); List>> outcomes = new ArrayList>>(); for (Map. Entry, Integer> entry : map.entrySet()) { outcomes.

Add(asMap(entry.getValue(), entry.getKey())); } return outcomes; } public Map asMap(K k, V v){Map result = new HashMap();result. Put(k, v);return result;} } – lacroix1547 Sep 5 '10 at 3:38 2 Wow, code in comments, what a mess. – lacroix1547 Sep 5 '10 at 3:38 Ill go read that covariant thing of Laurence now, maybe it will make me see the light.

– lacroix1547 Sep 5 '10 at 3:39 You're consistently declaring implementations instead of interfaces as generic types. Replace for example Map, Integer> map = new HashMap, Integer>(); by Map, Integer> map = new HashMap, Integer>();. – BalusC Sep 5 '10 at 3:43 See answer update.

– BalusC Sep 5 '10 at 3:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions