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 / General / October 2007

Tip: Looking for answers? Try searching our database.

2 different types in generic list

Thread view: 
Adam Lipscombe - 25 Oct 2007 12:55 GMT
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åkan Lane - 25 Oct 2007 13:05 GMT
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
Adam Lipscombe - 25 Oct 2007 14:35 GMT
>> 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
Lew - 25 Oct 2007 14:36 GMT
> 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.

Signature

Lew

Hendrik Maryns - 25 Oct 2007 15:39 GMT
Lew schreef:
>> 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
[quoted text clipped - 9 lines]
> <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.
Signature

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

Adam Lipscombe - 25 Oct 2007 16:16 GMT
> Lew schreef:
>>> I can't tell for sure why this does not work. The provided information
[quoted text clipped - 16 lines]
>
> H.

Very cool - works a treat.

Thanks!
Adam Lipscombe - 25 Oct 2007 15:56 GMT
OK thanks,

How do I addd to the collection?

TIA - Adam

>> 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
[quoted text clipped - 9 lines]
> <http://java.sun.com/docs/books/tutorial/extra/generics/morefun.html>
> and elsewhere.
Lew - 25 Oct 2007 16:08 GMT
> 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() );

Signature

Lew

Adam Lipscombe - 25 Oct 2007 16:15 GMT
>> OK thanks,
>>
[quoted text clipped - 9 lines]
>  List <Super> items = getFoos(); // would have to return List <Super>
>  items.addAll( getBars() );

OK thanks.


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.