Java nested wildcard generic won't compile?

So the problem is doSomething could be implemented as.

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

I have a problem with bounded nested wildcards in Java generics. Here's a common case: public void doSomething(Set set) {} public void callDoSomething() { Set set = new HashSet(); doSomething(set); } This is standard Java generics, works fine. However if the wildcard becomes nested, it no longer works: public void doSomething(Map> map) {} public void callDoSomething() { Map> map = new HashMap>(); doSomething(map); } This leads to a compiler error.

I've tried a variety of casts and wildcard permutations, but I'm unable to get this working. I don't recall seeing this issue before, and I've worked with generics for years. Am I just too tired and missing something obvious?

Java generics nested wildcard link|improve this question asked Aug 4 '10 at 13:15nilskp13719 50% accept rate.

So the problem is, doSomething could be implemented as: public void doSomething(Map> map) { Set set = ...; map. Put("xyz", set); } You need to decide what you actually mean. Probably something like: public void doSomething(Map> map) {}.

Yes, I just independently realized this. I could have sworn I tried that yesterday, but I probably had some other subtle change. – nilskp Aug 4 '10 at 13:34.

Similar kind of problem was answered in another forumn: forums.sun.com/thread.jspa?threadID=5377524 To make code to work Create HashMap as: Map> map = new HashMap>().

This will work for you: public void doSomething(Map> map) {}.

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