Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret Java generics like <T> T , <T,V> Query<T> , Class<T>?

Tags:

java

generics

There is Java API: Morphia

class Morphia defines method like:

<T> T fromDBObject(Class<T> entityClass, com.mongodb.DBObject dbObject)

Can some one explain what <T> T and Class<T> means? Is the method returning a class of type T in a collection?

Another API on Inteface DataStore:

<T,V> Query<T> find(Class<T> clazz, String property, V value) 

What <T,V> Query<T> means? Is the method returning an object of type Query that is then surrounded by collection `<T,V>` and `<T>`. This part is very confusing.

Is it correct to say whenever angle bracket (< > ) is involved, it always means a Java collection is involved?

Sorry I forgot to mark some content in this question as code otherwise SO was escaping changing entire meaning of the question and hence the 2 answers by @Vash and @fiver are in accordance to the question before this edit.

Thanks for pointing to some tutorials out there but please provide specifc answer from your expertise in Java generics to the question which will then help me to understand the tutorials better.

like image 760
ace Avatar asked Jun 28 '11 08:06

ace


People also ask

What does T stand for in Java generics?

< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.

What does the T indicate when used like list T >?

T means Type, for example if you want to create a list that has nothing but strings then this is how you do it. List <string> listname=new List<string>(); if you try to store anything dat of of any other datatype in the list , you will get an exception. To better understand the subject refer the topics on" Generics".

How do you use T generics in Java?

To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.

What is class T type in Java?

In java <T> means Generic class. A Generic Class is a class which can work on any type of data type or in other words we can say it is data type independent. public class Shape<T> { // T stands for "Type" private T t; public void set(T t) { this.t = t; } public T get() { return t; } }


1 Answers

Generics are not limited to collections but collections are good examples to understand why you need generics and how you can use them.

Assume that you create a new List object and you know it will only contain specific type of objects (e.g., MyClass objects). If you could somehow specify it in your code and it can be checked automatically then you can make sure that no other type of object will be inserted to this list. Moreover when other developers read your code, they easily understand that this list contains only `MyClass' objects.

Generics let us to include such information in our code without rewriting the List class. In fact, List functionality is independent from the type of objects it includes. So to create your own List you write this code:

         List<MyClass> myList = new List<MyClass>();

Now you can imagine cases where more than one generic type can be specified. A good example of this case is Map class. You can define the types of keys and values. So you see <K,V> in the Map class declaration (Map<K,V>).

In addition to Classes, generics can also be used in method/constructor declaration. In your example:

         <T,V> Query<T> find(Class<T> clazz, String property, V value) 

There is a method which has two generic types T and V and you can specify them (or they can be inferred by compiler) when you call the method. Here, <T,V> in the declaration explicitly indicates this method feature.

like image 100
salman.mirghasemi Avatar answered Oct 25 '22 14:10

salman.mirghasemi