0

I have a function which i want to make generic. I want below code

public String list(){
    List<EMovieCategory> dataList = DBCompanyContent.getInstance().getDataByDocType(EMovieCategory.class);
    return "";
}

Be something like

public String list(Class cls){
        List<WHAT TO WRITE HERE > dataList = DBCompanyContent.getInstance().getDataByDocType(cls);
        return "";
    }

I couldn`t figure out what to write inside List<***>

1
  • You should find some tutorial about Generic type in Java. Because if you get an answer, you might not understand it at all. Commented Sep 29, 2017 at 8:06

2 Answers 2

1

Use a type variable:

public <T> String list(Class<T> cls){
    List<T> dataList = DBCompanyContent.getInstance().getDataByDocType(cls);
    return "";
}

You might need to bound the type variable, if the getDataByDocType requires instances of Class within a certain bound:

public <T extends SomeClass> String list(Class<T> cls) {

OTOH, if you're not actually using the fact it's a list of Ts:

public String list(Class<?> cls){
    List<?> dataList = DBCompanyContent.getInstance().getDataByDocType(cls);
    return "";
}

or you don't even need a variable at all, if you're not going to use it:

public String list(Class<?> cls){
    DBCompanyContent.getInstance().getDataByDocType(cls);
    return "";
}
Sign up to request clarification or add additional context in comments.

Comments

0

The part between the <> is what data types the List will be storing. You'll be inputting what data type getDataByDocType(cls) returns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.