5. Java Generics, Object Methods, Lambdas & Streams

Chapters 17-20: Generic Classes/Wildcards, equals & hashCode contract, Lambdas, Functional Interfaces & Stream API

Module 1: Generics, Wildcards & equals/hashCode Contract

Achieving compile-time type safety and maintaining HashMap equality contracts.

🇬🇧 English Definition

Generics provide compile-time type safety. If two objects are equal according to equals(), they MUST return the same hashCode() to ensure correct HashSet/HashMap behavior.

🇮🇳 Hinglish Definition

Generics compile-time par type safety dete hain. Map/Set me correct working ke liye equals() aur hashCode() dono consistent hone chahiye.

Core Concepts & Rules

  • Generic Class: class Box { private T value; }
  • hashCode Contract: Equal objects must have identical hashCodes.

🔍 Line-by-Line Code Breakdown

  1. Line 1-5: Generic Box class storing value of parameterized type T.
  2. Line 8: Instantiating Box with type safety.

🎯 Interview Focus & Questions

  • Q: What is the equals and hashCode contract?
    A: If two objects are equal via equals(), their hashCode() values MUST be equal.
💻 Practical Java Code Example - Generics, Wildcards & equals/hashCode Contract
class Box<T> {
    private T value;
    public void set(T value) { this.value = value; }
    public T get() { return value; }
}

public class GenericsDemo {
    public static void main(String[] args) {
        Box<String> b = new Box<>();
        b.set("Java Generics");
        System.out.println(b.get());
    }
}
📤 Compiled Execution Output:
Java Generics

Module 2: Lambda Expressions & Stream API Processing

Functional programming pipelines using filter, map, collect, reduce, and distinct operations.

🇬🇧 English Definition

A Lambda expression is a concise implementation of a Functional Interface (@FunctionalInterface). Streams provide a declarative pipeline to filter, transform, collect, and reduce sequences of data.

🇮🇳 Hinglish Definition

Lambda expression anonymous behavior ko short form me likhti hai. Stream API collection data ko functional pipeline through filter, map aur collect karti hai.

Core Concepts & Rules

  • filter: Keeps elements matching predicate (n -> n % 2 == 0).
  • map: Transforms each element into another value (String::toUpperCase).
  • collect: Accumulates results into a List or Set (Collectors.toList()).

🔍 Line-by-Line Code Breakdown

  1. Line 2: List.of(1, 2, 3, 4) creates immutable list.
  2. Line 3-5: Stream pipeline filters even numbers, squares them, and prints results.

🎯 Interview Focus & Questions

  • Q: Intermediate vs Terminal Stream operations?
    A: Intermediate operations (filter, map) are lazy and return Streams; Terminal operations (collect, forEach) trigger execution.
💻 Practical Java Code Example - Lambda Expressions & Stream API Processing
import java.util.List;

public class StreamDemo {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4, 5);
        nums.stream()
            .filter(n -> n % 2 == 0)
            .map(n -> n * n)
            .forEach(System.out::println);
    }
}
📤 Compiled Execution Output:
4 16