Top 8 Java 8 Stream API Coding Interview Questions & Solutions
Java 8 Stream API is one of the most frequently asked topics during interviews for experienced Java developers. In this article, we'll solve some of the most commonly asked Java 8 coding interview questions with clean and optimized solutions.
1. Increase Employee Salary if Salary is Less than ₹20,000
Problem Statement
Given a list of employees, increase the salary by ₹10,000 for employees whose salary is less than ₹20,000 and print the updated employee list using Java 8 Streams.
Java Solution
Java 8import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("John", 15000),
new Employee("David", 28000),
new Employee("Steve", 18000),
new Employee("Alex", 35000)
);
employees.stream()
.map(emp -> {
if (emp.getSalary() < 20000) {
emp.setSalary(emp.getSalary() + 10000);
}
return emp;
})
.forEach(System.out::println);
}
}
Output
John -> 25000.0 David -> 28000.0 Steve -> 28000.0 Alex -> 35000.0
2. Remove Special Characters and Count Frequency of Each Word
Problem Statement
Given the following string, remove all special characters and print the frequency of each word using Java 8 Streams.
String data = "java@ #is &an ++Object";
Java Solution
Java 8import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String data = "java@ #is &an ++Object";
String cleaned = data.replaceAll("[^a-zA-Z ]", "");
Map<String, Long> frequency =
Arrays.stream(cleaned.split("\\s+"))
.filter(word -> !word.isEmpty())
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()
));
frequency.forEach((k, v) ->
System.out.println(k + " -> " + v));
}
}
Output
java -> 1 is -> 1 an -> 1 Object -> 1
3. Print Employees Whose Salary is Greater Than Their Manager
Problem Statement
Given a list of employees and their respective managers, print the employees whose salary is greater than their manager using Java 8 Streams.
Java Solution
Java 8import java.util.*;
class Employee {
String name;
double salary;
Employee manager;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
void setManager(Employee manager) {
this.manager = manager;
}
}
public class Main {
public static void main(String[] args) {
Employee manager = new Employee("John", 50000);
Employee emp1 = new Employee("David", 60000);
Employee emp2 = new Employee("Steve", 45000);
emp1.setManager(manager);
emp2.setManager(manager);
List<Employee> employees = Arrays.asList(emp1, emp2);
employees.stream()
.filter(e -> e.salary > e.manager.salary)
.forEach(e -> System.out.println(e.name));
}
}
Output
David
Explanation
The filter() operation compares each employee's salary with their manager's salary and prints only those employees whose salary is higher.
4. Group Anagrams from the List
Problem Statement
Group all the anagrams together using Java 8 Streams.
eat, tea, tan, ate, nat, bat
Java Solution
Java 8import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
List<String> words = Arrays.asList("eat", "tea", "tan", "ate", "nat", "bat");
Map<String, List<String>> result =
words.stream()
.collect(Collectors.groupingBy(word -> {
char[] chars = word.toCharArray();
Arrays.sort(chars);
return new String(chars);
}));
result.values().forEach(System.out::println);
}
}
Output
[eat, tea, ate] [tan, nat] [bat]
5. Find the Longest Common Prefix
Problem Statement
Find the longest common prefix from the given list of strings.
flower, flow, flight
Java Solution
Java 8public class Main {
static String longestPrefix(String[] arr) {
if (arr.length == 0) return "";
String prefix = arr[0];
for (int i = 1; i < arr.length; i++) {
while (!arr[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length() - 1);
}
}
return prefix;
}
public static void main(String[] args) {
String[] input = {"flower", "flow", "flight"};
System.out.println(longestPrefix(input));
}
}
Output
fl
Explanation
The prefix is reduced until every string starts with it.
6. Count Frequency of Characters
Problem Statement
Count the frequency of every character using Java 8 Streams.
String input = "Developer";
Java Solution
Java 8import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String input = "Developer";
input.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()))
.forEach((k, v) ->
System.out.println(k + " : " + v));
}
}
Output
D : 1 e : 3 v : 1 l : 1 o : 1 p : 1 r : 1
Explanation
Each character is converted into a stream and grouped using Collectors.groupingBy() to count its occurrences.
7. Optional Usage in Java 8 Streams
Problem Statement
Demonstrate how Optional helps avoid NullPointerException.
Java Solution
Java 8import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> name =
Optional.ofNullable("DevTechnoverse");
name.ifPresent(System.out::println);
}
}
Output
DevTechnoverse
Explanation
Optional safely wraps nullable objects and executes the operation only when a value is present.
8. Throw Exception Using Java 8 Streams
Problem Statement
Throw a custom exception while processing invalid data in a Java 8 Stream.
Java Solution
Java 8import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> numbers =
Arrays.asList("10", "20", "ABC");
numbers.stream()
.map(value -> {
try {
return Integer.parseInt(value);
} catch (Exception e) {
throw new RuntimeException("Invalid Number : " + value);
}
})
.forEach(System.out::println);
}
}
Output
10 20 Exception in thread "main" RuntimeException: Invalid Number : ABC
Explanation
The stream attempts to convert each value into an integer. If parsing fails, a custom RuntimeException is thrown with a meaningful message.





