Generics in Java with Examples

Generics is a feature that provides type safety in compile-time and gives us the ability to work with parameterized types.  We have generics classes and methods in Java.

Generic Class in Java

Declaring a generic class:

 public class GenericClass <T>


Creating an instance of a generic class:

 GenericClass<Type> obj = new GenericClass<Type>(); 


Looks familiar? We already have some in-built generic classes like ArrayList, HashMap, HashSet.

  List<String> list = new ArrayList<>();
  Map<Integer, String> map = new HashMap<>();
  Set<String> set = new HashSet<>();


When we have a generic class, we can use different types for the same variable inside the class.

Have a look at this example:

public class Test <T> {

  private T data;

  public Test(T data) {
    this.data = data;
  }

  public static void main(String[] args) {

    Test<Integer> test1 = new Test<>(10);

    Test<String> test2 = new Test<>("hello!");

    System.out.println(test1.data);
    System.out.println(test2.data);
  }

}
Output: 10 hello!
 
As you can see, we first assigned an int to the data field and then a String.
 
We can pass multiple type parameters also:

public class Test <T, U> {

  private T data1;
  private U data2;

  public Test(T data1, U data2) {
    this.data1 = data1;
    this.data2 = data2;
  }

  public static void main(String[] args) {

    Test<Integer, String> test1 = new Test<>(10, "dataString");

    Test<String, Integer> test2 = new Test<>("hello!", 10);

    System.out.println(test1.data1 + ", " + test1.data2);
    System.out.println(test2.data1 + ", " + test2.data2);
  }

}
Output: 10, dataString hello!, 10

Generic method in Java

We can write methods that can be called with different types of arguments. For example, a method can accept a parameter which type we don’t strictly specify.

Example

public class Test {

  public static void main(String[] args) {

    printParameter(10);
    printParameter("Some string");

  }

  private static <T> void printParameter(T param) {
    System.out.println(param);
  }

}
Output: 10 Some string
 
Here, we called the method printParameter() passing different types.
 
Note: We cannot use primitive data types like int, boolean, char with generics.
 
A method that returns a generic type:

public class Test {

  public static void main(String[] args) {

    Integer data1 = methodA(10);

    String data2 = methodA("Some string");

    System.out.println(data1);
    System.out.println(data2);

  }

  private static <T> T methodA(T param) {

    return param;
  }

}
Output: 10 Some string

Advantages of using Generics

  • Code reusability – We can write a method or a class once and use it for any type we want. 
  • Type Safety – We get the errors in compile time. If we specify a String when instantiating a class and later we try to provide an Integer type, we will get a compile-time error.

That was all about Generics in Java!

Leave a Reply

Your email address will not be published. Required fields are marked *