Ugenz Academy/Tutorial

Generics

4 min read739 words

Reading progress

Keep learning

Generics in Java

Introduction

Generics in Java allow us to write classes, interfaces, and methods that can work with different data types while providing type safety.

Generics are commonly used with Java Collections such as ArrayList, HashSet, and HashMap.

Example

ArrayList<String> names = new ArrayList<>();

Here, <String> specifies that the ArrayList can store only String values.

Why Use Generics?

Generics are used to:

  • Provide type safety.
  • Reduce type casting.
  • Detect errors at compile time.
  • Create reusable code.
  • Make collections easier to use.

Generic Syntax

ClassName<Type> objectName = new ClassName<>();

Example:

ArrayList<Integer> numbers = new ArrayList<>();

Example of Generics

import java.util.*; public class GenericExample { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); for (String name : names) { System.out.println(name); } } }

Output

Anu Ram Sita

Because the collection is declared as ArrayList<String>, only String values can be added.

Generic Class

A generic class is a class that can work with different data types.

Syntax

class ClassName<T> { T value; }

Here, T is a type parameter.

Example

class Box<T> { T value; Box(T value) { this.value = value; } void display() { System.out.println(value); } } public class GenericClassExample { public static void main(String[] args) { Box<Integer> number = new Box<>(100); Box<String> text = new Box<>("Hello"); number.display(); text.display(); } }

Output

100 Hello

The same Box class can store different data types.

Generic Method

A generic method can accept different types of data.

Example

public class GenericMethod { public static <T> void display(T value) { System.out.println(value); } public static void main(String[] args) { display(100); display("Anu"); display(10.5); } }

Output

100 Anu 10.5

Multiple Type Parameters

A generic class can have more than one type parameter.

class Student<K, V> { K id; V name; Student(K id, V name) { this.id = id; this.name = name; } void display() { System.out.println(id + " : " + name); } }

Example:

Student<Integer, String> student = new Student<>(101, "Anu"); student.display();

Output

101 : Anu

Common Generic Type Parameters

TypeMeaning
TType
EElement
KKey
VValue
NNumber

Example:

Map<Integer, String> students = new HashMap<>();

Here:

  • Integer → Key type
  • String → Value type

Generics with Collections

Generics are widely used with Java Collections.

ArrayList

ArrayList<String> names = new ArrayList<>();

HashSet

HashSet<Integer> numbers = new HashSet<>();

HashMap

HashMap<Integer, String> students = new HashMap<>();

Generic Class vs Normal Class

Generic ClassNormal Class
Can work with different data typesUsually designed for specific types
Provides type safetyMay require type casting
More reusableLess reusable
Uses type parametersDoes not use type parameters

Advantages of Generics

  1. Type Safety – Prevents incorrect data types.
  2. Code Reusability – Same code can work with different types.
  3. No Explicit Casting – Reduces the need for type casting.
  4. Compile-Time Checking – Type errors can be detected during compilation.
  5. Better Readability – Makes the expected data type clear.

Example Without Generics

ArrayList list = new ArrayList(); list.add("Anu"); list.add(100); String name = (String) list.get(0);

Casting is required.

Example With Generics

ArrayList<String> list = new ArrayList<>(); list.add("Anu"); list.add("Ram"); String name = list.get(0);

No explicit casting is required.

Important Points

  • Generics were introduced to provide type safety and reusable code.
  • Generics use angle brackets < >.
  • Common type parameters are T, E, K, and V.
  • Generics work with classes, methods, and interfaces.
  • Generics are heavily used in the Java Collection Framework.
  • Primitive types such as int cannot be used directly as generic type arguments; use wrapper classes such as Integer.

Conclusion

Generics are an important feature of Java that allow classes and methods to work with different data types while maintaining type safety. They make Java programs more reusable, readable, and easier to maintain, especially when working with collections.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy