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
| Type | Meaning |
|---|---|
T | Type |
E | Element |
K | Key |
V | Value |
N | Number |
Example:
Map<Integer, String> students = new HashMap<>();
Here:
Integer→ Key typeString→ 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 Class | Normal Class |
|---|---|
| Can work with different data types | Usually designed for specific types |
| Provides type safety | May require type casting |
| More reusable | Less reusable |
| Uses type parameters | Does not use type parameters |
Advantages of Generics
- Type Safety – Prevents incorrect data types.
- Code Reusability – Same code can work with different types.
- No Explicit Casting – Reduces the need for type casting.
- Compile-Time Checking – Type errors can be detected during compilation.
- 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, andV. - Generics work with classes, methods, and interfaces.
- Generics are heavily used in the Java Collection Framework.
- Primitive types such as
intcannot be used directly as generic type arguments; use wrapper classes such asInteger.
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.