1. What does Collectors.groupingBy do by default?
Default `groupingBy(classifier)` uses a `HashMap<K, List<T>>`. Sorting or concurrent maps require additional downstream or mapFactory arguments.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
Java · Question Set
Java Streams & Functional Programming interview questions for placements and exams.
Questions
15
Included in this set
Subject
Java
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for Java.
Default `groupingBy(classifier)` uses a `HashMap<K, List<T>>`. Sorting or concurrent maps require additional downstream or mapFactory arguments.
For complete preparation, combine this set with full subject-wise practice for Java. You can also explore other subjects and sets from the links below.
Lambdas close over effectively final variables, just like anonymous classes. They do not require generating a new class per use and can target any compatible functional interface.
There are four forms: static, instance of particular object, instance of arbitrary object of a particular type, and constructor. `objectRef::instanceMethod` is the particular object form.
Streams are lazy: intermediate steps are recorded until a terminal operation triggers evaluation. There’s exactly one terminal operation per pipeline.
Mapping, filtering, sorting build the pipeline; collect, reduce, forEach consume it. `peek` is intermediate and `allMatch` is terminal.
Use `map` for one-to-one transformations. Use `flatMap` for one-to-many where inner streams or collections are concatenated into a single stream.
`distinct` tracks seen elements to remove duplicates, requiring state. `sorted` is also stateful. `filter`, `map`, and `peek` are stateless.
Parallelism shines on big, CPU-bound, pure operations with minimal coordination. Blocking I/O, synchronization, or strict encounter order often negate benefits or harm performance.
`collect` is built for mutable result containers and supports parallel-friendly accumulation and combination. `reduce` targets immutable, associative reductions.
Good: return Optional from methods that may be empty; use terminal operations like orElse, orElseGet, orElseThrow instead of get(). Good: chain map/flatMap/filter to avoid nested ifs. Anti-patterns: use Optional for fields or parameters (adds overhead and ambiguity); call get() without checking presence; use Optional in performance-critical collections.
Primitive streams save allocations and add numeric terminals (sum, average, min, max). They convert via mapToInt, mapToLong, mapToDouble and boxed().
Side effects (e.g., mutating external state in map/filter) break referential transparency, hinder parallelization, and cause subtle bugs. Prefer pure functions and let `collect` build results. If you must observe, use `peek` only for diagnostics; for imperative accumulation, a simple loop may be clearer.
Streams are single-use. Create a new stream from the source for additional terminals, or collect data to a reusable structure first.
A functional interface is defined by a single abstract method. @FunctionalInterface is optional and only enforces the rule at compile time. Default and static methods don’t change the SAM count.
Encounter order is defined by the source; unordered sources can produce arbitrary visitation order. Use `sorted` or ordered sources (e.g., List, LinkedHashSet) when order matters, or `forEachOrdered` in parallel.