Introduction to Java Generics and their usage with Collections

Piyu Jain
Javarevisited
Published in
3 min readMay 6, 2023

Java Generics were introduced in Java 5 to provide better type safety and reusability of code. Generics are a way to specify a type parameter to a class or method, which can be replaced by any valid type at the time of instantiation. In this article, we will discuss the basics of Java Generics and how they are used with collections.

Introduction to Java Generics

Java Generics are a way to make code more flexible and reusable by allowing classes and methods to operate on a wide range of types rather than on specific types. Generics allow you to specify a type parameter to a class or method that can be replaced by any valid type at the time of instantiation.

For example, suppose you have a method that operates on an array of integers:

public static int sum(int[] arr) {
int sum = 0;
for (int i : arr) {
sum += i;
}
return sum;
}

If you want to use this method for an array of doubles, you would have to create a new method that takes an array of doubles as a parameter:

public static double sum(double[] arr) {
double sum = 0.0;
for (double d : arr) {
sum += d;
}
return sum;
}

This can quickly become cumbersome if you need to create a separate method for each data type. This is where Generics come in handy.

With Generics, you can create a single method that can operate on any type of array:

public static <T extends Number> double sum(T[] arr) {
double sum = 0.0;
for (T t : arr) {
sum += t.doubleValue();
}
return sum;
}

In the above example, we have used a type parameter T that extends the Number class. This means that we can pass an array of any class that extends Number, such as Integer, Double, or Float, to this method.

How Generics are used with collections:

Generics are heavily used with collections in Java. They allow you to create collections that can hold any type of object, while still providing type safety at compile-time.

For example, suppose you want to create a List that can hold any type of object:

List<Object> list = new ArrayList<Object>();

In the above example, we have created a List of Object type. This means that we can add any type of object to this list, including Strings, Integers, and even custom objects.

However, if we try to add an object of the wrong type to this list, the compiler will throw an error at compile-time:

list.add("Hello");  // Compilation error

This is because the list is of type Object, and we are trying to add a String to it.

To avoid this error, we can use Generics to specify the type of objects that the List can hold:

List<String> list = new ArrayList<String>();

In the above example, we have created a List of String type. This means that we can only add String objects to this list.

This provides type safety at compile-time, and also makes our code more readable and maintainable.

In conclusion, Java Generics is a powerful feature that allows you to write more flexible and reusable code. They are heavily used with collections in Java to provide type safety and make code more readable and maintainable.

--

--

Piyu Jain
Javarevisited

Write about #Java #Technology, #AI, #Book Summary, #Motivation, #Earning