Java generics question with wildcards
Posted
by Sean
on Stack Overflow
See other posts from Stack Overflow
or by Sean
Published on 2010-06-09T16:54:35Z
Indexed on
2010/06/09
17:02 UTC
Read the original article
Hit count: 216
Just came across a place where I'd like to use generics and I'm not sure how to make it work the way I want.
I have a method in my data layer that does a query and returns a list of objects. Here's the signature.
public List getList(Class cls, Map query)
This is what I'd like the calling code to look like.
List<Whatever> list = getList(WhateverImpl.class, query);
I'd like to make it so that I don't have to cast this to a List coming out, which leads me to this.
public <T> List<T> getList(Class<T> cls, Map query)
But now I have the problem that what I get out is always the concrete List<WhateverImpl>
passed in whereas I'd like it to be the Whatever
interface. I tried to use the super keyword but couldn't figure it out. Any generics gurus out there know how this can be done?
© Stack Overflow or respective owner