Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Problem with Widcards

Thread view: 
astyskin@gmail.com - 27 Feb 2006 14:09 GMT
I cannot understand the error.

Test.java:24: test(java.util.Map<java.lang.Long,java.util.List<?>>) in
Test cannot be applied to
(java.util.Map<java.lang.Long,java.util.List<java.lang.Double>>)
   test(map);
   ^
1 error

What am I doing wrong?

As an example here is a code:
import java.util.*;

public class Test {
 static void test(Map<Long, List<?>> map) {
   for(Long id : map.keySet()) {
     System.out.println(id);
     for(Object obj : map.get(id)) {
       System.out.println(obj);
     }
   }
 }

 public static void main(String[] args) {
   Map<Long, List<Double>> map = new TreeMap<Long, List<Double>>();
   for(long i = 1; i < 5; i++) {
     List<Double> array = new ArrayList<Double>();
     for(int j=3; j < 6; j++) {
       array.add((double)i*j);
     }
     map.put(i, array);
   }
   
   test(map);    
 }
}
IchBin - 27 Feb 2006 15:51 GMT
> I cannot understand the error.
>
[quoted text clipped - 12 lines]
> public class Test {
>   static void test(Map<Long, List<?>> map) {

static void test(Map<Long, List<Double>> map) {

>  
>     for(Long id : map.keySet()) {
[quoted text clipped - 11 lines]
>       for(int j=3; j < 6; j++) {
>         array.add((double)i*j);

array.add( new Double( i * j));

>       }
>       map.put(i, array);
[quoted text clipped - 3 lines]
>   }
> }

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Hendrik Maryns - 02 Mar 2006 20:23 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

astyskin@gmail.com uitte de volgende tekst op 02/27/2006 03:09 PM:
> I cannot understand the error.
>
[quoted text clipped - 33 lines]
>   }
> }

Working with multilevel generics is very tricky.  You want test() to
accept any Map which has Longs as keys and any subtype of List as
values.  What you have above only accepts Maps that have Longs as keys
and Lists of *the specific type List<?>* as keys.  What you want is

test(Map<Long, ? extends List<?>>).

Have a look at Angelika Langer?s excellent FAQ about Java generics,
specifically the question ?How do I implement a method that takes a
multi-level wildcard argument??:
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#How
%20do%20I%20implement%20a%20method%20that%20takes%20a%20multi-level%20wildcard%2
0argument
?,
also under http://tinyurl.com/o4a82.

Hope this helps, H.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org

Hendrik Maryns - 02 Mar 2006 20:31 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Hendrik Maryns uitte de volgende tekst op 03/02/2006 09:23 PM:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 43 lines]
> values.  What you have above only accepts Maps that have Longs as keys
> and Lists of *the specific type List<?>* as keys.  

That?s wrong.  The problem is, that Map<Long, List<?>> can map Longs on
any type of list.  I.e., 3 could map to a List<String>, whereas 5 maps
to List<Number> and so on.  Thus, the moment you give Java a Map<Long,
List<Double>>, it says: cannot assign, because imagine that this would
work, then the following code would be possible, and of course we don?t
want that:

Map<Long, List<?>> badMap = new HashMap<Long, List<Double>>;  //will not
compile
map.put(5, new ArrayList<String>());

Because, if you look at the type of Map, it can accept any map as value.
Because the assignment of actual arguments to formal arguments in a
method invocation is the same as a normal assignment, this is rejected.

The rest of my answer is correct.

> What you want is
>
[quoted text clipped - 7 lines]
>
> Hope this helps, H.

Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.