File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic List type conversions

 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an interface, Human, and a bunch of subclasses: Users, Managers, Clowns, etc.

My utility functions return

List<? extends Human>

Some of the public functions want to return List of specific types, say a List<User>

What is the cleanest way to downcast the result so we can return List<User> when the worker function returns a

List<? extends Human> which is actually a List< humans who are actually User object>

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your question exactly, but you can just cast it:

There's no way to avoid a cast somewhere in this case.
 
Rob Spoor
Sheriff
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the cast will issue a warning, because the List may be a List<Manager> instead of a List<User>.

Pat, do you need to add objects to your List? If no, then using List<? extends User> is just fine.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the warning is a problem, you can suppress the warning by using the @SuppressWarnings annotation:
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:But the cast will issue a warning, because the List may be a List<Manager> instead of a List<User>.

Pat, do you need to add objects to your List? If no, then using List<? extends User> is just fine.




Right, the list may actually contain Ranchers, since Ranchers are Human (usually)
Only some functions explicitly return List<Rancher> others return List<User>

Not sure I grok your suggestion, are you saying that the getUsers() function should return List<? extends User>
 
Rob Spoor
Sheriff
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant List<? extends Human> of course.

How about using a Class as an extra parameter?
If you pass User.class the method returns List<User>, if you pass Manager.class the method returns List<Manager>, etc. A quick example:
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:How about using a Class as an extra parameter?


It works, but I really dislike using the Class as an extra parameter to fix flaws in the way Generics work, so I do it only when I really, really have to.

Its a minor thing, but shows how they grafted generics onto Java long after the initial design. The curse of backwards compatibility.

I'll just use the cast as someone mentioned upthread.

Thanks to all
 
Rob Spoor
Sheriff
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use type inference if your body can deal with it:
The biggest problem with this code is that you cannot use T for creating objects or arrays or any type checking; instanceof T is not possible, nor are new T() and new T[].
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic