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 / May 2007

Tip: Looking for answers? Try searching our database.

Casting a Generic List

Thread view: 
Bryan - 26 Apr 2007 16:11 GMT
Hello all,

Is it possible to cast an entire Generic List somehow?  For example,
say I have the following:

public interface Foo {
   .....
}

public class Bar implements Foo {
   ....
}

public class Test {
   public List<Foo> getFooList() { .... }

   public static void main(String[] args) {
       Test test = new Test();

       List<Bar> bars = test.getFooList();  // illegal
   }
}

Now assume that all the objects in the FooList were indeed
instantiated as Bar objects.

I get a compile error saying it can't convert from List<Foo> to
List<Bar>.  I could always just get a list of Foo objects and cast
them to Bars myself, but I'm just wondering if it can be done for me
all at once.

Any suggestions?
Ingo R. Homann - 26 Apr 2007 16:24 GMT
HI,

> Hello all,
>
[quoted text clipped - 28 lines]
>
> Any suggestions?

Not every Foo is a Bar, so of course such an assignment (or even a cast)
is impossible. (Note that for a similar (but slightly more difficult)
reason the opposite cast is impossible as well.)

Ciao,
Ingo
Daniel Pitts - 27 Apr 2007 02:01 GMT
> Hello all,
>
[quoted text clipped - 31 lines]
>
> Any suggestions?

If you know that every Foo in the foo list is a Bar, then the only way
to get a List<Bar> object is to copy all elements from one to the
other, casting as you go.

You can "cheat", but you'd likely introduce bugs.  List<Bar> bars
=(List<Bars>)(List)foos;
In general, don't do this.

This brings up the point.  If you know that every Foo in a list is a
Bar, why not start with a List<Bar> instead of List<Foo>

Even if you don't know that before a class is instantiated, there are
ways around it.

class FooHolder<T extends Foo> {
  private List<T> list;
  // ...

   public List<T> getFoos() {
       return list;
   }
   public static void main(String...args) {
       FooHolder<Bar> fh = new FooHolder<Bar>();
       // Look ma, no casting!
       List<Bar> bars = fh.getFoos();
   }
}
Bryan - 02 May 2007 14:06 GMT
Daniel,

Awesome... your second suggestion is what I was looking for.

Thank you very much!

Bryan

> If you know that every Foo in the foo list is a Bar, then the only way
> to get a List<Bar> object is to copy all elements from one to the
[quoted text clipped - 24 lines]
>
> }


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



©2009 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.