Skip to content

Latest commit

 

History

History
100 lines (75 loc) · 3.6 KB

File metadata and controls

100 lines (75 loc) · 3.6 KB

Q11 — New Features of Java 8

Interview Tip: Don't just list them — show 2-3 with live code. Lambda + Streams + Optional are the top 3 to demonstrate.


📌 Key Java 8 Features

Feature What it does
Lambda Expression Anonymous function — shorter way to implement functional interfaces
Stream API Pipeline operations on collections (filter, map, reduce)
Optional Avoids NullPointerException — wrapper for nullable values
Functional Interfaces Interface with single abstract method — @FunctionalInterface
Default Methods Methods with body inside interface
Method References ClassName::methodName shorthand for lambdas
Date/Time API LocalDate, LocalDateTime, ZonedDateTime — replaces Date
Collectors Utility to collect stream results

💻 Code — Run This Live

import java.util.*;
import java.util.stream.*;
import java.util.Optional;
import java.time.LocalDate;

public class Java8Features {

    public static void main(String[] args) {

        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Dave", "Anna");

        // 1. Lambda + forEach
        System.out.println("=== Lambda ===");
        names.forEach(name -> System.out.println(name));

        // 2. Stream API
        System.out.println("\n=== Streams ===");
        List<String> filtered = names.stream()
            .filter(name -> name.startsWith("A"))     // filter
            .map(String::toUpperCase)                  // transform
            .sorted()                                  // sort
            .collect(Collectors.toList());             // collect
        System.out.println(filtered);  // [ALICE, ANNA]

        // 3. Optional
        System.out.println("\n=== Optional ===");
        Optional<String> opt = Optional.ofNullable(null);
        String result = opt.orElse("Default Value");
        System.out.println(result);  // Default Value

        Optional<String> present = Optional.of("Hello");
        present.ifPresent(v -> System.out.println("Value: " + v));

        // 4. Method Reference
        System.out.println("\n=== Method Reference ===");
        names.forEach(System.out::println);  // same as n -> System.out.println(n)

        // 5. Default Method in Interface
        // (see interface below)

        // 6. New Date API
        System.out.println("\n=== Date/Time API ===");
        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.of(1995, 8, 15);
        System.out.println("Today: " + today);
        System.out.println("Days since birthday: " +
            java.time.temporal.ChronoUnit.DAYS.between(birthday, today));
    }
}

// Default Method in Interface
interface Greeting {
    String greet(String name);  // abstract

    default String shout(String name) {   // default — has body
        return greet(name).toUpperCase();
    }
}

📌 Key Points

  • Lambda (param) -> expression → implements functional interface in one line
  • Stream → lazy evaluation, doesn't modify original collection
  • Optional → use orElse(), orElseThrow(), map(), ifPresent()
  • Default methods → added to avoid breaking existing implementations when new methods added to interface
  • Method references → 4 types: static, instance, constructor, arbitrary instance
  • Java 8 Date API → immutable and thread-safe (unlike old Date/Calendar)

⚠️ Common Interview Catch

"Is Stream lazy?" → Yes. Intermediate operations (filter, map) are lazy — they run only when terminal operation (collect, forEach, findFirst) is called.