What are the uses of generic collections in Java?



What are Generic Collections in Java?

In Java, the Generic collections were introduced in Java 5. These collections disable the type-casting, and there is no need for explicit type-casting if we use generic collections.

The generic collections are type-safe and detect type-related errors at compile time. It allows the datatypes to pass as parameters to classes or interfaces. The Compiler is responsible for checking the compatibility of the types.

Syntax

Following is the way to create generic collections in Java:

class<Type>
or
interface<Type>

Where type specifies the type of the object, such as: Integer, String, Character, etc.. You can create generic collections using both collection interfaces and classes:

Usage of the Generic Collections in Java:

Here are a few important uses of generic collections in Java:

Checks for Type safety

In Java, the Generic collections allow a single type of object, which means that you cannot insert multiple types of values in a single object.

If the object is created using a specific type, such as integer, string, character, etc. This ensures the type safety.

Example

The following example shows how Generic collections prevent adding multiple types of values in a single object:

import java.util.ArrayList;
import java.util.List;

public class genericCollections{
   public static void main(String[] args) {
      // before generics
      List list = new ArrayList();
      list.add(10);
      
      //no error
      list.add("100");
      
      //after adding generics
      List<Integer> list1 = new ArrayList<Integer>();
      list1.add(10);
      
      // compile-time error.
      list1.add("100");
   }
}

The above program produces the following output:

genericCollections.java:18: error: incompatible types: String cannot be converted to Integer
      list1.add("100");
                ^
Note: genericCollections.java uses unchecked or unsafe operations.

Avoid Type Casting

In Java, typecasting is a technique that is used to convert values from one type to another, such as from an int to a String, from a char to a String, and so on.

However, the Generic collections help to avoid the need for type casting because it specifies the type (i.e., Integer, String, Character, etc.) while creating an object.

Example

In the following example, we use the toString() method for typecasting the List value, whereas it also shows that the Generic collections do not require any typecasting:

import java.util.ArrayList;
import java.util.List;

public class genericCollections{
   public static void main(String[] args) {
      //before generics
      List list = new ArrayList();
      list.add(10);
      list.add("100");
      
      //need typecasting
      String str = list.get(1).toString();
      System.out.println("Value = " + str);
      
      //adding generics
      List<String> list1 = new ArrayList<String>();
      list1.add("Mango");
      list1.add("Apple");
      list1.add("Banana");
      
      //no need of type casting
      String fruit = list1.get(0);
      System.err.println("Fruit = " + fruit);
   }
}

Below is the output of the above code:

Value = 100
Fruit = Mango

Check Errors at Compile-time

In Java, compile-time errors are detected during the compilation process. The Generic collections enable type safety by detecting the type-related errors at compile time.

Example

The following example usage of the Generic collections to detect the type-related errors at compile-time:

import java.util.ArrayList;
import java.util.List;

public class genericCollections{
   public static void main(String[] args) {
      //before generics
      List list = new ArrayList();
      list.add(10);

      //no type safety, you can add any type value
      list.add("100");

      //after generic collections
      List<Integer> list1 = new ArrayList<>();
      //adding values to it
      list1.add(10);
      list1.add(20);

      //adding different type of value
      list1.add("fruit");
   }
}

Following is the output of the above program:

genericCollections.java:20: error: incompatible types: String cannot be converted to Integer
      list1.add("fruit");
                ^
Note: genericCollections.java uses unchecked or unsafe operations.
or
The method add(Integer) in the type List<Integer> is not applicable for the arguments (String)
Updated on: 2025-06-18T18:30:32+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements