- Type safety
- Avoid type csting (It offen happens in the Lists.)
- Compile-Time checking
- Java Generics using Map
Short note :
Map<Integer,String> map=new HashMap<Integer,String>();Long note :
import java.util.*;
class TestGenerics{
public static void main(String args[]){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(1,"vijay");
map.put(4,"umesh");
map.put(2,"ankit");
//Now use Map.Entry for Set and Iterator
Set<Map.Entry<Integer,String>> set=map.entrySet();
Iterator<Map.Entry<Integer,String>> itr=set.iterator();
while(itr.hasNext()){
Map.Entry e=itr.next();//no need to typecast
System.out.println(e.getKey()+" "+e.getValue());
}
}
}- Generic class we can use generic types in our classes. The T type indicates that it can refer to any type (like String, Integer, and Employee). The type you specify for the class will be used to store and retrieve the data.
class MyGenContainer<T>{
T obj;
void add(T obj){
this.obj=obj;
}
T get(){
return obj;
}
} Note : The type parameters naming conventions are important to learn generics thoroughly. The common type parameters are as follows:
T - Type E - Element K - Key N - Number V - Value
- Generic Method Like the generic class, we can create a generic method that can accept any type of arguments. Here, the scope of arguments is limited to the method where it is declared. It allows static as well as non-static methods.
public class GenericsMethods {
//Java Generic Method
public static <T> boolean isEqual(GenericsType<T> g1, GenericsType<T> g2){
return g1.get().equals(g2.get());
}
public static void main(String args[]){
GenericsType<String> g1 = new GenericsType<>();
g1.set("Pankaj");
GenericsType<String> g2 = new GenericsType<>();
g2.set("Pankaj");
boolean isEqual = GenericsMethods.<String>isEqual(g1, g2);
//above statement can be written simply as
isEqual = GenericsMethods.isEqual(g1, g2);
//This feature, known as type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.
//Compiler will infer the type that is needed
}
}we use Wildcards to put a restriction on our generics to control it and prevent unwanted business. we do it by using wildcard element. The ? (question mark) symbol represents the wildcard element. It means any type. If we write <? extends Number>, it means any child class of Number, e.g., Integer, Float, and double. Now we can call the method of Number class through any child class object.
We can use a wildcard as a type of a parameter, field, return type, or local variable. However, it is not allowed to use a wildcard as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
we have 3 types of wildcard usage in Java Generics :
- Unbounded Wildcards (no inheritance)
- Upper Bounded Wildcards (using child classes by using extends)
- Lower Bounded Wildcards ((using parent classes by using super))