Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: 2–5 Years

Revise the Fresher Tier

  • Revise: Core Java Fundamentals (Fresher Tier)
  • Revise: Collections, Threads, Java 8 & Coding (Fresher Tier)
  • Revise: Spring Core, Spring Boot & Spring MVC (Fresher Tier)
  • Revise: Hibernate, SQL, Microservices, Maven & Git (Fresher Tier)

Intermediate Core Java

  • Intermediate OOP & Language Features — Interview Questions
  • Enums, Generics, Pass-by-Value & Imports — Interview Questions
  • Collections Internals & Scenarios — Interview Questions
  • JVM, Memory & Class Loading — Interview Questions
  • Exception Design & Serialization Edge Cases — Interview Questions

Java 8 Deep Dive & Stream Coding

  • Lambdas, Functional Interfaces & Method References — Interview Questions
  • Default Methods, Backward Compatibility & Optional — Interview Questions
  • Stream API Internals (Part 1) — Interview Questions
  • Stream API Internals (Part 2) — Interview Questions
  • Stream Coding Problems (Level 2, Part 1) — Interview Questions
  • Stream Coding Problems (Level 2, Part 2) — Interview Questions

Concurrency & Multithreading

  • Synchronization, Locks & Deadlocks — Interview Questions
  • Executors, ThreadLocal & Concurrent Collections — Interview Questions

Spring Framework In Depth

  • Bean Lifecycle, Contexts & Circular Dependencies — Interview Questions
  • Spring Bean Conflicts, AOP, Batch & Injection Styles — Interview Questions

Spring Boot In Depth & Scenarios

  • Spring Boot Internals & Auto-Configuration — Interview Questions
  • Spring Boot Configuration, Profiles & Secrets — Interview Questions
  • Spring Boot Data, Transactions & Caching — Interview Questions
  • Spring Boot Testing, Error Pages & Deployment — Interview Questions
  • Spring Boot Performance, Scaling & Resilience — Interview Questions
  • Spring Boot Async, Events & Messaging — Interview Questions
  • Spring Boot Security Scenarios — Interview Questions
  • Actuator, AOP, Spring Cloud & Distributed Tracing — Interview Questions
  • External APIs, Files, GraphQL & WebFlux — Interview Questions

Spring Security

  • Spring Security — OAuth2, CORS, CSRF & Access Rules — Interview Questions
  • Spring Security — Passwords, Filter Chain, Sessions & Debugging — Interview Questions

Apache Kafka

  • Kafka Architecture, Topics & ZooKeeper vs KRaft — Interview Questions
  • Kafka Producers, Consumers, Reliability & Streams — Interview Questions
  • Kafka Security, Connect & Core Scenarios — Interview Questions
  • Kafka Production Scenarios — Interview Questions

Git, Maven & Gradle (Level II)

  • Git Workflows, Rebase & Conflict Handling — Interview Questions
  • Git Recovery, Hooks, Tags & Collaboration — Interview Questions
  • Maven Builds, Multi-Module Projects & Dependency Resolution — Interview Questions
  • Maven Profiles, settings.xml, Plugins & Quality Gates — Interview Questions
  • Gradle Fundamentals, Migration & Dependencies — Interview Questions
  • Gradle Performance, Plugins, Testing & Publishing — Interview Questions

Deployment & CI/CD

  • Sessions, CI/CD Pipelines, Rollbacks & Secrets — Interview Questions
  • Migrations, Zero-Downtime Releases, Containers & Monitoring — Interview Questions

JUnit 5 & Mockito

  • JUnit 5 — Interview Questions
  • Mockito Basics — Interview Questions
  • Mockito Advanced & Tricky Questions — Interview Questions
Chaturmind
← Java Interview Prep: 2–5 Years

Revise the Fresher Tier

  • Revise: Core Java Fundamentals (Fresher Tier)
  • Revise: Collections, Threads, Java 8 & Coding (Fresher Tier)
  • Revise: Spring Core, Spring Boot & Spring MVC (Fresher Tier)
  • Revise: Hibernate, SQL, Microservices, Maven & Git (Fresher Tier)

Intermediate Core Java

  • Intermediate OOP & Language Features — Interview Questions
  • Enums, Generics, Pass-by-Value & Imports — Interview Questions
  • Collections Internals & Scenarios — Interview Questions
  • JVM, Memory & Class Loading — Interview Questions
  • Exception Design & Serialization Edge Cases — Interview Questions

Java 8 Deep Dive & Stream Coding

  • Lambdas, Functional Interfaces & Method References — Interview Questions
  • Default Methods, Backward Compatibility & Optional — Interview Questions
  • Stream API Internals (Part 1) — Interview Questions
  • Stream API Internals (Part 2) — Interview Questions
  • Stream Coding Problems (Level 2, Part 1) — Interview Questions
  • Stream Coding Problems (Level 2, Part 2) — Interview Questions

Concurrency & Multithreading

  • Synchronization, Locks & Deadlocks — Interview Questions
  • Executors, ThreadLocal & Concurrent Collections — Interview Questions

Spring Framework In Depth

  • Bean Lifecycle, Contexts & Circular Dependencies — Interview Questions
  • Spring Bean Conflicts, AOP, Batch & Injection Styles — Interview Questions

Spring Boot In Depth & Scenarios

  • Spring Boot Internals & Auto-Configuration — Interview Questions
  • Spring Boot Configuration, Profiles & Secrets — Interview Questions
  • Spring Boot Data, Transactions & Caching — Interview Questions
  • Spring Boot Testing, Error Pages & Deployment — Interview Questions
  • Spring Boot Performance, Scaling & Resilience — Interview Questions
  • Spring Boot Async, Events & Messaging — Interview Questions
  • Spring Boot Security Scenarios — Interview Questions
  • Actuator, AOP, Spring Cloud & Distributed Tracing — Interview Questions
  • External APIs, Files, GraphQL & WebFlux — Interview Questions

Spring Security

  • Spring Security — OAuth2, CORS, CSRF & Access Rules — Interview Questions
  • Spring Security — Passwords, Filter Chain, Sessions & Debugging — Interview Questions

Apache Kafka

  • Kafka Architecture, Topics & ZooKeeper vs KRaft — Interview Questions
  • Kafka Producers, Consumers, Reliability & Streams — Interview Questions
  • Kafka Security, Connect & Core Scenarios — Interview Questions
  • Kafka Production Scenarios — Interview Questions

Git, Maven & Gradle (Level II)

  • Git Workflows, Rebase & Conflict Handling — Interview Questions
  • Git Recovery, Hooks, Tags & Collaboration — Interview Questions
  • Maven Builds, Multi-Module Projects & Dependency Resolution — Interview Questions
  • Maven Profiles, settings.xml, Plugins & Quality Gates — Interview Questions
  • Gradle Fundamentals, Migration & Dependencies — Interview Questions
  • Gradle Performance, Plugins, Testing & Publishing — Interview Questions

Deployment & CI/CD

  • Sessions, CI/CD Pipelines, Rollbacks & Secrets — Interview Questions
  • Migrations, Zero-Downtime Releases, Containers & Monitoring — Interview Questions

JUnit 5 & Mockito

  • JUnit 5 — Interview Questions
  • Mockito Basics — Interview Questions
  • Mockito Advanced & Tricky Questions — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: 2–5 YearsJava 8 Deep Dive & Stream Coding
✓ FreeIntermediate· 7 min read

Stream Coding Problems (Level 2, Part 2) — Interview Questions

Current date and time with java.time, concatenating streams, cube-and-filter, sorting an array then streaming it, uppercase mapping, list-to-map with duplicate keys in sorted order, word counts, duplicates with counts, null-safe list iteration with Optional, max of an array, and character counts.

Published September 25, 2026


How to use this lesson

The second half of the Level 2 stream problems. Pay attention to the three places where commonly shared answers are wrong: a typo'd lambda, a misleading comment about which duplicate "wins" in toMap, and map(note -> Notes::getTagName), which doesn't compile. Knowing why they're wrong is great interview material.

Q1. How do you get the current date and time with the Java 8 Date-Time API?

LocalDate today = LocalDate.now();                        // 2026-09-25 (no time, no zone)
LocalTime now = LocalTime.now();                          // 14:03:27.118
LocalDateTime local = LocalDateTime.now();                // date + time, but NO time zone
ZonedDateTime inIndia = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
Instant instant = Instant.now();                          // a UTC point on the timeline: use for timestamps
String formatted = local.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"));

Key points to cover:

  • Store and transmit Instant (or OffsetDateTime) for events. LocalDateTime has no zone, so it's ambiguous across servers.
  • Inject a Clock (LocalDate.now(clock)) to make time-dependent code testable.
  • The java.time types are immutable and thread-safe, unlike Date, Calendar and SimpleDateFormat.

Q2. How do you concatenate two streams?

Stream<String> all = Stream.concat(list1.stream(), list2.stream());
List<String> merged = Stream.of(list1, list2, list3).flatMap(List::stream).toList();   // three or more sources

Key points to cover:

  • Stream.concat is lazy. Deeply nested concat calls (in a loop) can cause a StackOverflowError. Use flatMap for many sources.

Q3. Cube the elements of a list, and keep the results greater than 50.

List<Integer> cubes = List.of(4, 5, 6, 7, 1, 2, 3).stream()
        .map(i -> i * i * i)
        .filter(c -> c > 50)
        .toList();                                         // [64, 125, 216, 343]

Key points to cover:

  • Order matters for efficiency. If you could filter on the input first, that avoids computing values that get thrown away. Here the condition depends on the cube, so map must come first.
  • For large inputs, cubes overflow int above 1290. Use mapToLong(i -> (long) i * i * i).

Q4. Sort an array, then convert it into a stream.

int[] arr = { 99, 55, 203, 99, 4, 91 };
Arrays.sort(arr);                                          // or Arrays.parallelSort(arr) for very large arrays
Arrays.stream(arr).forEach(n -> System.out.print(n + " "));   // 4 55 91 99 99 203

int[] sortedCopy = Arrays.stream(arr).sorted().toArray();  // without mutating the original

Common trap: the widely shared version writes forEach(n > System.out.print(...)). That's a typo for ->, and it doesn't compile. Also, parallelSort only pays off for large arrays (thousands of elements).

Q5. Use map to convert strings to uppercase.

List<String> upper = names.stream().map(s -> s.toUpperCase(Locale.ROOT)).toList();   // [AA, BB, CC, DD]

Key points to cover:

  • String::toUpperCase uses the default locale. Pass Locale.ROOT for identifiers and codes, to avoid locale surprises (the Turkish dotless i).

Q6. Convert a list of objects to a map, handling duplicate keys, in sorted order.

Short answer: Use toMap with a merge function and a map supplier. Here, notes are keyed by tag name, and the map is ordered by tag ID, descending:

record Note(int id, String tagName, long tagId) { }

List<Note> notes = List.of(new Note(1, "note1", 11), new Note(2, "note2", 22), new Note(3, "note3", 33),
                           new Note(4, "note4", 44), new Note(5, "note5", 55), new Note(6, "note4", 66));

Map<String, Long> byTag = notes.stream()
        .sorted(Comparator.comparingLong(Note::tagId).reversed())      // 66, 55, 44, 33, 22, 11
        .collect(Collectors.toMap(Note::tagName, Note::tagId,
                                  (first, second) -> first,           // keep the FIRST one encountered…
                                  LinkedHashMap::new));               // …and keep insertion (sorted) order
// {note4=66, note5=55, note3=33, note2=22, note1=11}

Common trap: believing the comment "keeps 44 for the duplicate key". The stream is sorted descending, so note4 with 66 is encountered first, and the merge function keeps 66. Always reason about the encounter order when you choose (a, b) -> a or (a, b) -> b. For keys sorted by name instead, use TreeMap::new.

Q7. Count each word in a list of strings.

List<String> names = List.of("AA", "BB", "AA", "CC");
Map<String, Long> counts = names.stream()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));   // {AA=2, BB=1, CC=1} (HashMap order)

Map<String, Long> sortedCounts = names.stream()
        .collect(Collectors.groupingBy(Function.identity(), TreeMap::new, Collectors.counting()));

Q8. Find only the duplicate elements, with their counts.

Map<String, Long> duplicates = names.stream()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
        .entrySet().stream()
        .filter(e -> e.getValue() > 1)
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));    // {AA=2}

Key points to cover:

  • The alternative, filter(x -> Collections.frequency(names, x) > 1), is O(n²). It's fine for tiny lists, but it's a red flag on large ones. Counting once with groupingBy is O(n).

Q9. If a list may be null or empty, iterate it safely with Optional and print each element.

Optional.ofNullable(notes)
        .orElseGet(List::of)                        // null → empty list
        .stream()
        .filter(Objects::nonNull)                   // skip null elements
        .map(Note::tagName)                         // a method reference
        .forEach(System.out::println);

Common trap: the popular answer uses .map(note -> Notes::getTagName). That's a lambda returning a method reference, so it doesn't compile (the target type isn't a functional interface). Write map(Note::tagName) or map(n -> n.tagName()).

Key points to cover:

  • The better fix is upstream: never return null collections. Return an empty list, and callers need no Optional gymnastics. Optional.ofNullable(list) is a workaround for legacy APIs.

Q10. Find the maximum element in an array.

static int findMax(int[] arr) {
    return Arrays.stream(arr).max()
                 .orElseThrow(() -> new IllegalArgumentException("array is empty"));
}
// [12, 19, 20, 88, 0, 9] → 88

Key points to cover:

  • getAsInt() on an empty OptionalInt throws NoSuchElementException, with no context. Prefer orElseThrow with a message.
  • IntStream.summaryStatistics() gives min, max, sum and average in one pass.

Q11. Count each character in a string.

String s = "string data to count each character";

Map<Character, Long> counts = s.toLowerCase(Locale.ROOT).chars()
        .filter(Character::isLetter)                                   // skip spaces (clarify with the interviewer)
        .mapToObj(c -> (char) c)
        .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
// {s=1, t=5, r=3, i=1, n=2, g=1, d=1, a=5, o=2, c=4, u=1, e=2, h=2}

Key points to cover:

  • chars() avoids the split("") approach, which creates a String object per character.
  • For characters outside the Basic Multilingual Plane (emoji), use codePoints().

Follow-up questions this topic invites — and their answers

Q: How do you find the day of the week, or add business days, with java.time? A: date.getDayOfWeek(). For business days, iterate with plusDays while skipping SATURDAY/SUNDAY (and holidays from a calendar), or use a TemporalAdjuster.

Q: How do you convert Date to LocalDateTime? A: date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(). Be explicit about the zone.

Q: How do you join the elements of a list into a comma-separated string? A: list.stream().map(String::valueOf).collect(Collectors.joining(", ")), or String.join(", ", stringList) when you already have strings.

Q: How do you get the top 3 most frequent words? A: Count with groupingBy(identity(), counting()), then stream the entry set, sort with Map.Entry.<String, Long>comparingByValue().reversed(), limit(3), and map to the keys.

Previous

Stream Coding Problems (Level 2, Part 1) — Interview Questions

Next

Synchronization, Locks & Deadlocks — Interview Questions

AI Tutor

Lesson: Stream Coding Problems (Level 2, Part 2) — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.