Learn Java
import com.learnjava.datastructurescollections.GenericsInCollections;

Generics in Collections in Java

@Concept

Generics let a class or method be written once but work with any type, specified in angle brackets at the point of use — ArrayList<String> is an ArrayList specifically of String elements, and ArrayList<Integer> is a completely separate specialization of that same general-purpose class. The compiler checks generic type usage at compile time and enforces it: attempting to add an Integer to an ArrayList<String> simply fails to compile, catching an entire category of mistake before the program ever runs.

Before generics existed, pre-Java 5, collections stored plain Object references and needed an explicit cast every time an element was retrieved, with no compile-time guarantee the cast would actually succeed. Generics remove both the manual casting and that runtime risk in one change.

@HowItWorks
1

The angle-bracket syntax

ArrayList<String> declares an ArrayList specifically restricted to holding String elements.

2

Compile-time type checking

Adding an element of the wrong type to a generic collection fails to compile, rather than failing only once the program runs.

3

No manual casting needed

Retrieving an element from a generic collection already comes back as the declared type, with no cast required.

4

Before generics (pre-Java 5)

Collections stored plain Object, requiring an explicit cast on every retrieval, with no compile-time safety net if that cast was wrong.

5

Generic type parameters can be almost any class

Including custom ones a program defines itself, not just built-in types like String or Integer.

@Example
import java.util.ArrayList;
 
public class GenericsDemo {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Ana");
String first = names.get(0);
System.out.println("First name: " + first);
System.out.println("Length: " + first.length());
}
}
Output
First name: Ana
Length: 3
1names is declared as an ArrayList of String specifically, so only String values can ever be added to it.
2names.get(0) already returns a String directly — no cast is needed, unlike pre-generics Java, which would have returned a plain Object here.
3first.length() can be called immediately on that returned value, since the compiler already knows its exact type.
4Both println calls run once each, giving two lines of output.
@UseCases
Catching a wrong-type addition to a collection at compile time instead of at runtime
Reading elements from a collection without needing an explicit cast on every retrieval
Writing a custom class or method that works with any type, using a generic type parameter
Understanding old pre-generics Java code that casts every element retrieved from a collection

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.