Sean
Sean

Reputation: 902

Java generics question with wildcards

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<Whatever> 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?

Upvotes: 5

Views: 310

Answers (3)

Andrei Fierbinteanu
Andrei Fierbinteanu

Reputation: 7826

The only way I can think this would work is like this:

public <T extends Whatever> List<? extends Whatever> get(Class<T> clazz, Map query) {
 return new ArrayList<T>(); // do whatever it is you do here to return a List<T>
}

and then call it like:

List<? extends Whatever> l = get(WhateverImpl.class, query);
for (Whatever w : l) {
    // do something
}

You can't return List<Whatever> directly since you won't be able to cast List<WhateverImpl> to it inside the get method (they're not compatible).

Upvotes: 2

JSBձոգչ
JSBձոգչ

Reputation: 41388

Is there any reason that you can't just do this?

public List<T> getList<T>(Map query)

Upvotes: -1

Joachim Sauer
Joachim Sauer

Reputation: 308249

You need to define the method like this:

public <B, C extends B> List<B> getList(final Class<C> cls, final Map<?, ?> query)

Upvotes: 8

Related Questions