I am designing a simple Data Access Object for my Java application. I have a few classes (records) that represents a single row in tables like User
and Fr
You are pretty close.
public <T> LinkedList<T> getAllRecords(List<T> list) {
...
}
This is called a Generic Method.
You will want to specify a parameter like List<T>
. Then, based upon the type of the list you pass in, Java will infer the generic type to return.
Edit:
Poly's answer is very good. It should be easy enough for you to do the following and not have to create a TypeReference
class.
List<Fruit> fruit = myDataAccessObject.getAllRecrods(new LinkedList<Fruit>());
List<User> users = myDataAccessObject.getAllRecords(new LinkedList<User>());