AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with Widcards

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon Feb 27, 2006 2:12 pm    Post subject: Problem with Widcards Reply with quote



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);
}
}
Back to top
IchBin
Guest





PostPosted: Mon Feb 27, 2006 4:12 pm    Post subject: Re: Problem with Widcards Reply with quote



astyskin (AT) gmail (DOT) com wrote:
Quote:
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) {

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

Quote:

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);

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

Quote:
}
map.put(i, array);
}

test(map);
}
}



--

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-)
Back to top
Hendrik Maryns
Guest





PostPosted: Thu Mar 02, 2006 9:12 pm    Post subject: Re: Problem with Widcards Reply with quote



-----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:
Quote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

astyskin (AT) gmail (DOT) com uitte de volgende tekst op 02/27/2006 03:09 PM:
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);
}
}

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.

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.

Quote:
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%20argument?,
also under http://tinyurl.com/o4a82.

Hope this helps, H.


--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEB1YXiOEY3xKMFEERAnd6AKDEPyD4zIVmFknJ01sZfB/zi57wCgCfUOUl
4H7ADyuv75+AFAalDDcPPB8=
=6C/u
-----END PGP SIGNATURE-----
Back to top
Hendrik Maryns
Guest





PostPosted: Thu Mar 02, 2006 9:12 pm    Post subject: Re: Problem with Widcards Reply with quote

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

astyskin (AT) gmail (DOT) com uitte de volgende tekst op 02/27/2006 03:09 PM:
Quote:
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);
}
}

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%20argument?,
also under http://tinyurl.com/o4a82.

Hope this helps, H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEB1QxiOEY3xKMFEERAs/JAJ41E7IvQlvG/QJiUHaGU/ZF+wzhQACeLZj3
AiboznhvN7rNzRzZ7RZ08ck=
=xKN0
-----END PGP SIGNATURE-----
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.