KatInterview

Modern Java 8 Toolkit: Lambdas, Streams, Optional, java.time

Arrays & HashingModel answer
MediumModel answerArrays & Hashingjavajava-8lambdas

Last updated

Explain the major Java 8 language and library additions, when each one fits, and the common pitfalls, using short code examples.

Java 8 was a major release that brought functional-style programming to the language while remaining backward compatible.

Give a structured overview of its most important additions. For each of the five areas below, explain what it is, when you would reach for it, and what mistakes people commonly make. Include a short code example for each.

  1. Lambda expressions (and method references)
  2. The Stream API
  3. Optional
  4. Default (and static) methods in interfaces
  5. The java.time date and time API

Also explain how these features relate to one another, and briefly mention any smaller additions you consider worth knowing.

Hints

Hint 1

Start with the dependency chain: lambdas need functional interfaces, streams are driven by lambdas, and default methods are how existing interfaces such as Collection gained stream() without breaking implementers.

Hint 2

For each feature, give one 'use it when' case and one 'do not use it when' case, for example Optional as a return type versus Optional as a field or parameter.

Hint 3

Remember the classic gotchas: effectively final captured variables, streams being single-use and lazy, Optional.of(null) throwing, orElse versus orElseGet, the diamond problem for default methods, and Instant versus LocalDateTime.