Skip to main content

The Java 8 Interview Guide

· 7 min read

Everything a Senior Java Interviewer Expects You to Know

I have seen Java evolve from the early JDK days to modern cloud native architectures. But if there is one release that fundamentally changed how Java is written, it is Java 8.

Before Java 8, Java was powerful but verbose. Writing simple operations on collections required boilerplate loops, anonymous classes, and a lot of ceremony.

Then Java 8 arrived.

And suddenly Java developers were talking about:

  • Functional programming
  • Lambdas
  • Streams
  • Optional
  • Immutable date APIs

From an interviewer’s perspective, Java 8 became a major filter. Candidates who truly understood it stood out immediately.

This guide walks through everything a strong backend engineer must know about Java 8.


Why Java 8 Was a Game Changer

Before Java 8, Java lacked features that other languages already had:

  • Functional programming
  • Clean collection processing
  • Immutable date APIs
  • Asynchronous computation primitives

Developers had to write verbose code for simple tasks.

Example: Filtering a list before Java 8

List<Integer> evenNumbers = new ArrayList<>();

for(Integer num : numbers){
if(num % 2 == 0){
evenNumbers.add(num);
}
}

With Java 8:

List<Integer> evenNumbers =
numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());

Less code. More expressive. Easier to parallelize.


Problems with Pre Java 8 Java

1. Boilerplate Code

Anonymous classes were everywhere.

Collections.sort(list, new Comparator<Integer>() {
public int compare(Integer a, Integer b){
return a - b;
}
});

Java 8 simplified this drastically.


2. No Functional Programming Support

Languages like Scala and JavaScript already supported:

  • first class functions
  • lambda expressions
  • function composition

Java lacked these features until Java 8.


Functional Programming in Java

Functional programming focuses on:

  • pure functions
  • immutability
  • higher order functions

Java introduced functional programming through:

  • Lambdas
  • Functional interfaces
  • Streams API

Lambda Expressions

A lambda expression represents an anonymous function.

Syntax

(parameters) -> expression

Example:

(a, b) -> a + b

Used with functional interfaces.

Example with comparator:

Collections.sort(list, (a, b) -> a - b);

Functional Style Programming

Instead of telling the computer how to do something, you describe what needs to happen.

Imperative:

for(int i : list){
if(i > 10) result.add(i);
}

Functional:

list.stream()
.filter(i -> i > 10)
.collect(Collectors.toList());

Behind The Scenes Compilation

A common interview question:

Are lambda expressions anonymous classes?

No.

Java compiles lambdas using invokedynamic bytecode instruction and LambdaMetafactory.

Advantages:

  • better performance
  • reduced class generation
  • improved memory usage

Interview Question

Q: What is the difference between lambda and anonymous class?

Key differences:

  1. Lambdas do not create a new scope for this
  2. Lambdas are compiled using invokedynamic
  3. Anonymous classes generate class files

Functional Interfaces

A functional interface contains exactly one abstract method.

Example:

interface Calculator {
int add(int a, int b);
}

@FunctionalInterface

Optional annotation.

@FunctionalInterface
interface MyFunction {
void execute();
}

Compiler ensures only one abstract method exists.


Built in Functional Interfaces

Java provides several interfaces in java.util.function.

Important ones interviewers expect you to know:

InterfaceDescription
Predicate<T>Represents a boolean condition
Function<T, R>Transforms a value
Consumer<T>Accepts a value
Supplier<T>Provides or returns a value
UnaryOperator<T>Operation on a single operand of the same type
BinaryOperator<T>Operation on two operands of the same type

Example:

Predicate<Integer> isEven = n -> n % 2 == 0;

Custom Functional Interface

@FunctionalInterface
interface Greeting {
void sayHello(String name);
}

Usage:

Greeting g = name -> System.out.println("Hello " + name);

Interview Trap

Question:

Is Runnable a functional interface?

Yes.

Runnable r = () -> System.out.println("Running");

Method References

Method references allow referencing methods directly.

Syntax:

ClassName::methodName

Static Method Reference

Integer::parseInt

Example:

list.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());

Instance Method Reference

String::toLowerCase

Constructor Reference

User::new

Example:

Supplier<User> supplier = User::new;

Streams API

Streams process collections in a functional style pipeline.


What Problem Streams Solve

Before Java 8:

  • iteration was external
  • logic mixed with data traversal

Streams introduced internal iteration.


External vs Internal Iteration

External:

for(int i : list)

Internal:

list.stream().filter(...)

Lazy Evaluation

Intermediate operations execute only when terminal operation runs.

Example:

list.stream()
.filter(x -> x > 5)
.map(x -> x * 2)
.collect(Collectors.toList());

Stream Pipeline

Source -> Intermediate Operations -> Terminal Operation

Example:

List -> filter -> map -> collect

Intermediate Operations

filter

list.stream()
.filter(x -> x > 10)

map

Transforms elements.

list.stream()
.map(x -> x * 2)

flatMap

Flattens nested structures.

list.stream()
.flatMap(List::stream)

distinct

Removes duplicates.

stream.distinct()

sorted

stream.sorted()

limit

stream.limit(5)

skip

stream.skip(3)

peek

Used for debugging.

stream.peek(System.out::println)

Terminal Operations

collect

stream.collect(Collectors.toList())

reduce

Used for aggregation.

int sum = list.stream()
.reduce(0, Integer::sum);

findFirst

stream.findFirst()

anyMatch

stream.anyMatch(x -> x > 10)

allMatch

stream.allMatch(x -> x > 0)

noneMatch

stream.noneMatch(x -> x < 0)

count

stream.count()

min / max

stream.max(Integer::compareTo)

Collectors in Depth

Collectors transform streams into collections.


toList

stream.collect(Collectors.toList())

toSet

stream.collect(Collectors.toSet())

toMap

stream.collect(Collectors.toMap(
User::getId,
User::getName
));

groupingBy

Map<String, List<User>> users =
users.stream()
.collect(Collectors.groupingBy(User::getDepartment));

partitioningBy

Map<Boolean, List<Integer>> result =
list.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));

joining

String result =
list.stream()
.collect(Collectors.joining(","));

counting

Collectors.counting()

summarizingInt

IntSummaryStatistics stats =
list.stream()
.collect(Collectors.summarizingInt(Integer::intValue));

Optional Class

Optional represents a container that may or may not contain a value.


Why Optional Was Introduced

Java suffered heavily from:

NullPointerException

Optional forces developers to handle absence of value.


Example

Optional<String> name = Optional.of("Java");

map vs flatMap

map transforms inside Optional.

flatMap avoids nested Optional.


orElse vs orElseGet vs orElseThrow

orElse(value)

always evaluates.

orElseGet(supplier)

lazy execution.

orElseThrow()

throws exception.


Default Methods in Interfaces

Java 8 allows methods with implementation inside interfaces.

Example:

interface Vehicle {

default void start(){
System.out.println("Starting");
}
}

Diamond Problem

If two interfaces define the same default method:

Class must override it.


Static Methods in Interfaces

Interfaces can contain static methods.

Example:

interface MathUtil {

static int add(int a,int b){
return a+b;
}
}

Call using:

MathUtil.add(1,2);

New Date Time API (java.time)

Old APIs:

  • Date
  • Calendar

Problems:

  • mutable
  • thread unsafe
  • confusing

LocalDate

LocalDate.now()

LocalTime

LocalTime.now()

LocalDateTime

LocalDateTime.now()

ZonedDateTime

Supports timezones.

ZonedDateTime.now()

Duration

Used for time based difference.


Period

Used for date difference.


Formatting

DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd-MM-yyyy");

Parallel Streams

Parallel streams allow parallel processing.

list.parallelStream()

Internal Working

Uses ForkJoinPool.commonPool().

Work divided across threads.


When NOT to Use

  • IO operations
  • small collections
  • shared mutable state

Nashorn JavaScript Engine

Java 8 introduced Nashorn to run JavaScript inside JVM.

Example:

ScriptEngine engine =
new ScriptEngineManager().getEngineByName("nashorn");

Type Inference Improvements

Java 8 improved type inference for generics and lambdas.


Java 8 forEach

list.forEach(System.out::println);

Base64 Encoding

Java 8 added Base64 utilities.

Base64.getEncoder().encodeToString(data);

CompletableFuture

CompletableFuture enables asynchronous programming.

Example:

CompletableFuture.supplyAsync(() -> {
return "Hello";
}).thenApply(String::toUpperCase)
.thenAccept(System.out::println);

Real Interview Coding Questions

Find duplicate elements

list.stream()
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() > 1)

Convert List to Map

Map<Integer, User> map =
users.stream()
.collect(Collectors.toMap(
User::getId,
Function.identity()
));

Top Java 8 Interview Questions

  1. What is a functional interface
  2. Difference between map and flatMap
  3. Difference between stream and parallel stream
  4. What is lazy evaluation
  5. What happens when multiple terminal operations are used
  6. Difference between Optional.of and Optional.ofNullable
  7. Difference between reduce and collect
  8. What is invokedynamic
  9. What is method reference
  10. When should you use Optional

Common Java 8 Interview Mistakes

  1. Overusing parallel streams
  2. Using streams for simple loops
  3. Misusing Optional as field variables
  4. Ignoring immutability
  5. Not understanding lazy evaluation

Java 8 Interview Cheat Sheet

Core concepts interviewers check:

  • Functional interfaces
  • Lambda expressions
  • Streams pipeline
  • Optional
  • Method references
  • Default methods
  • CompletableFuture
  • Date Time API
  • Collectors
  • Parallel streams

If a developer understands these deeply, they can handle nearly every Java 8 interview question.