aspose file tools*
The moose likes Java in General and the fly likes generic List type conversions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "generic List type conversions" Watch "generic List type conversions" New topic
Author

generic List type conversions

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

If the warning is a problem, you can suppress the warning by using the @SuppressWarnings annotation:
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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
 
subject: generic List type conversions