10 Common Java Interview Questions Related to the Stream API
Java interview questions related to streams are often focused on practical usage of the Stream API introduced in Java 8. Some of the most common practical questions include:
1. Filtering a List
Problem: Given a list of integers, filter out the even numbers and return a list of odd numbers.
Solution:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> oddNumbers = numbers.stream()
.filter(n -> n % 2 != 0)
.collect(Collectors.toList());
}
}
2. Mapping a List
Problem: Given a list of strings, convert all strings to uppercase.
Solution:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = Arrays.asList("apple", "banana", "cherry");
List<String> uppercasedWords = words.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
}
}
3. Sorting a List
Problem: Given a list of integers, sort the list in ascending order.
Solution:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 3, 8, 1);
List<Integer> sortedNumbers = numbers.stream()
.sorted()
.collect(Collectors.toList());
}
}
4. Finding Maximum/Minimum Value
Problem: Given a list of integers, find the maximum number.
Solution:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Integer max = numbers.stream()
.max(Integer::compareTo)
.orElse(null);
}
}
5. Summing Values in a List
Problem: Given a list of integers, calculate the sum of all the numbers.
Solution:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.mapToInt(Integer::intValue)
.sum();
}
}
6. Checking if All or Any Elements Match a Condition
Problem: Check if all numbers in a list are greater than 10.
Solution:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(12, 15, 18);
boolean allGreaterThan10 = numbers.stream()
.allMatch(n -> n > 10);
}
}
7. Reducing a List
Problem: Given a list of integers, find the product of all the numbers.
Solution:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
int product = numbers.stream()
.reduce(1, (a, b) -> a * b);
}
}
8. Grouping Data by a Property
Problem: Given a list of Person objects, group them by age.
Solution:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Person> people = Arrays.asList(new Person("Alice", 30),
new Person("Bob", 25));
Map<Integer, List<Person>> groupedByAge = people.stream()
.collect(Collectors.groupingBy(Person::getAge));
}
}
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
9. Flat Mapping a List of Lists
Problem: Given a list of lists of integers, flatten them into a single list.
Solution:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<List<Integer>> listOfLists = Arrays.asList(Arrays.asList(1, 2),
Arrays.asList(3, 4));
List<Integer> flattenedList = listOfLists.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
}
}
10. Working with Optional
Problem: Given a list of integers, find the first even number or return a default value.
Solution:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 3, 4, 5);
Integer firstEven = numbers.stream()
.filter(n -> n % 2 == 0)
.findFirst()
.orElse(-1);
}
}