2 different types in generic list

A

Adam Lipscombe

Folks,


I am converting existing code to use paramaterised generic collections.
I need to add 2 different object types to a common list. Both object are interfaces that extend a
common interface.


e..:

Assuming MyType, MyTypeA, MyTypeB, where MyTypeA and MyTypeB are interfaces that extend interface
MyType.

List<? extends MyType> list = getMyTypeAList();
List.addAll(getMyTypeBList());


The compiler barfs at the addAll() as follows:

"The method addAll(Collection<? extends capture#1-of ? extends Bean>) in the type List<capture#1-of
? extends Bean> is not applicable for the arguments (List<MyTypeB>)"

I dont understand why as interface MyTypeB extends interface MyType.



I have also tried it with:

List<MyType> = getMyTypeAList();
List.addAll(getMyTypeBList());

This time the compiler complains "Type mismatch: cannot convert from List<MyTypeA> to List<Bean>"



What am doing to wrong? How to do this?


TIA - Adam
 
H

Håkan Lane

I can't tell for sure why this does not work. The provided information
is hard to interpret. Please send some source code. What I would think
can work, though, is if you make MyTypeA and MyTypeB subclasses of a
common superclass implementing the required interface. You would then
have a collection of instances of this superclass. Did you consider that
option? It seems to be a classical case of inheritance.

Håkan Lane
 
A

Adam Lipscombe

Please send some source code.


Here it is:


List<Claim> claimList = ClaimBO.getIncompleteClaimsList(user);
List<? extends Bean> items = claimList;

List<Trip> tripList = TripBO.getIncompleteTripsList(user);
items.addAll(tripList); // ** Compilter error here **




ClaimBO.getIncompleteClaimsList(user) definition:
public static List<Claim> getIncompleteClaimsList(User user)

Claim definition:
public interface Claim extends Bean, Cloneable



TripBO.getIncompleteTripsList(user) definition:
public static List<Trip> getIncompleteTripsList(User user)

Trip definition:
public interface Trip extends Bean, Cloneable



Compiler error: "The method addAll(Collection<? extends capture#1-of ? extends Bean>) in the type
List<capture#1-of ? extends Bean> is not applicable for the arguments (List<Trip>)"



Any ideas?


TIA - Adam
 
L

Lew

HÃ¥kan Lane said:
I can't tell for sure why this does not work. The provided information
is hard to interpret. Please send some source code. What I would think
can work, though, is if you make MyTypeA and MyTypeB subclasses of a
common superclass implementing the required interface. You would then
have a collection of instances of this superclass. Did you consider that
option? It seems to be a classical case of inheritance.

He did make those types both subtypes of a common supertype already.

The problem is that ? extends Foo allows getting from the collection but not
adding to it, for reasons explained in
<http://java.sun.com/docs/books/tutorial/extra/generics/morefun.html>
and elsewhere.
 
H

Hendrik Maryns

Lew schreef:
He did make those types both subtypes of a common supertype already.

The problem is that ? extends Foo allows getting from the collection but
not adding to it, for reasons explained in
<http://java.sun.com/docs/books/tutorial/extra/generics/morefun.html>
and elsewhere.

So the solution is to copy over the list when you get it:

List<MyType> list = new ArrayList<MyType>(getMyTypeAList());
List.addAll(getMyTypeBList());

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHIKqle+7xMGD3itQRAgesAJ0eENqxnDBMNojFjw5XOO+LXxCDcwCfaXDe
Lde7pPPy+fi+bfRRUSFmqwg=
=UKED
-----END PGP SIGNATURE-----
 
L

Lew

Adam said:
OK thanks,

How do I addd to the collection?

Please do not top-post. Review the FAQ post that appears here every few days,
and the links to which it points.

One way is to drop the wildcard altogether.

Given Foo and Bar both implementing Super:

List <Super> items = getFoos(); // would have to return List <Super>
items.addAll( getBars() );
 
A

Adam Lipscombe

Lew said:
Please do not top-post. Review the FAQ post that appears here every few
days, and the links to which it points.

One way is to drop the wildcard altogether.

Given Foo and Bar both implementing Super:

List <Super> items = getFoos(); // would have to return List <Super>
items.addAll( getBars() );


OK thanks.
 
A

Adam Lipscombe

Hendrik said:
Lew schreef:

So the solution is to copy over the list when you get it:

List<MyType> list = new ArrayList<MyType>(getMyTypeAList());
List.addAll(getMyTypeBList());

H.


Very cool - works a treat.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top