Java Functional Java
The largest change in how Java is written, and the source of the most confidently wrong interview answers — laziness, parallelism, and checked exceptions in lambdas.
6 concepts · 16 interview questions
What this topic covers
Every concept in functional java, and the questions each one gets asked as. Where a question links, it has a full write-up.
Lambdas and functional interfaces
A lambda implements a single-method interface. No built-in functional interface declares throws, which is what made checked exceptions unusable in new APIs.
- What makes an interface functional?
- How is a lambda different from an anonymous inner class?
- What are the four kinds of method reference?
Streams and laziness
Intermediate operations only build a pipeline; nothing runs until a terminal operation. A stream is single-use.
- Why does a stream with no terminal operation do nothing?
- What is the difference between an intermediate and a terminal operation?
- Can you reuse a stream?
Collectors
groupingBy, partitioningBy and toMap differ mainly in their failure modes — toMap throws on duplicate keys and NPEs on a null value.
- How do Collectors.groupingBy and toMap differ in failure modes?
- How do you write a custom collector?
- What does flatMap do?
Parallel streams
parallelStream uses one shared ForkJoinPool for the whole JVM, so one slow task starves everything else using it.
- When is parallelStream() a mistake?
- Which pool does a parallel stream use?
- What makes an operation safe to parallelise?
Exceptions in streams
A checked exception cannot escape a lambda, so every workaround converts it to unchecked at the boundary.
- How do you handle a checked exception inside a stream?
- Should you use a stream when you need early exit?
Default and static interface methods
Default methods exist so interfaces like Collection could gain methods without breaking every implementor — and they brought back a diamond problem.
- Why were default methods added, and what did they break?
- What happens when two interfaces provide the same default method?