Forums Register Login

generic List type conversions

+Pie Number of slices to send: Send
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>

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
If the warning is a problem, you can suppress the warning by using the @SuppressWarnings annotation:
+Pie Number of slices to send: Send
 

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>
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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[].
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1548 times.
Similar Threads
SCJP 5 K&B question problem
doubt in Struts application
Generics Question:
Wildcard
Generics question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:56:15.