Java8 Stream API Coding Interview Questions (Part-I)
Find out all even and odd numbers from a given list of integers using streams.
package com.java.streams; import java.util.Arrays; import java.util.List; public class EvenOddExample { private static void findEvenNumbers(List<Integer> integerList) { integerList.stream() .filter(n -> n%2==0) .forEach(System.out::println); } private static void findOddNumbers(List<Integer> integerList) { integerList.stream() .filter(n -> n%2!=0) .forEach(System.out::println); } public static void main(String[] args) { System.out.println("Even Numbers Are: "); findEvenNumbers(Arrays.asList(10,15,8,49,25,98,98,32,15)); System.out.println("Odd Numbers Are: "); findOddNumbers(Arrays.asList(10,15,8,49,25,98,98,32,15)); } } Output: Even Numbers Are: 10 8 98 98 32 Odd Numbers Are: 15 49 25 15
Identify all numbers from a provided list of integers that start with either 1 or 9 using streams.
package com.java.streams; import java.util.Arrays; import java.util.List; public class NumberStartsWith { private static void findStartsWithOne(List<Integer> integerList) { integerList.stream() .map(n -> String.valueOf(n)) .filter(s -> s.startsWith("1") || s.startsWith("9")) .forEach(System.out::println); } public static void main(String[] args) { System.out.println("Numbers Start With 1 & 9 Are:"); findStartsWithOne(Arrays.asList(10,15,8,49,25,98,98,32,15)); } } Output: Numbers Start With 1 & 9 Are: 10 15 98 98 15
Using streams, identify duplicate, unique, and non-duplicate numbers from a provided list of integers.
package com.java.streams; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; public class DuplicateElements { private static void findDuplicate(List<Integer> integerList) { Set<Integer> integerSet = new HashSet<>(); integerList.stream() .filter(n -> !integerSet.add(n)) .forEach(System.out::println); } private static void findUnique(List<Integer> integerList) { integerList.stream() .distinct() .forEach(System.out::println); } private static void findNonDuplicate(List<Integer> integerList) { integerList.stream() .filter(n -> Collections.frequency(integerList, n)==1) .forEach(System.out::println); } public static void main(String[] args) { System.out.println("Duplicate Elements:"); findDuplicate(Arrays.asList(10,15,8,49,25,98,98,32,15)); System.out.println("Unique Elements:"); findUnique(Arrays.asList(10,15,8,49,25,98,98,32,15)); System.out.println("Non Duplicate Elements:"); findNonDuplicate(Arrays.asList(10,15,8,49,25,98,98,32,15)); } } Output: Duplicate Elements: 98 15 Unique Elements: 10 15 8 49 25 98 32 Non Duplicate Elements: 10 8 49 25 32
Find the first and any element from a list of integers using streams.
package com.java.streams;
import java.util.Arrays;
import java.util.List;
public class FindFirstAnyExample {
private static void findFirst(List<Integer> integerList) {
integerList.stream()
.findFirst()
.ifPresent(System.out::println);
}
private static void findAny(List<Integer> integerList) {
integerList.stream()
.findAny()
.ifPresent(System.out::println);
}
public static void main(String[] args) {
System.out.println("First Element:");
findFirst(Arrays.asList(49,25,88,88,32,15,10,15,18));
System.out.println("Any Element:");
findAny(Arrays.asList(49,25,88,88,32,15,10,15,18));
}
}
Output:
Find First Element:
49
Find Any Element:
49
- Using streams, determine the maximum and minimum values present in a given list of integers.
package com.java.streams;
import java.util.Arrays;
import java.util.List;
public class MaxMinValueElementExample {
private static void findMax(List<Integer> integerList) {
int maxValue = integerList.stream()
.max(Integer::compare)
.get();
System.out.println(maxValue);
}
private static void findMin(List<Integer> integerList) {
int minValue = integerList.stream()
.min(Integer::compare)
.get();
System.out.println(minValue);
}
public static void main(String[] args) {
System.out.println("Max Element:");
findMax(Arrays.asList(49,25,88,88,32,15,10,15,18));
System.out.println("Min Element:");
findMin(Arrays.asList(49,25,88,88,32,15,10,15,18));
}
}
Output:
Max Element:
88
Min Element:
10
- Sort the values in ascending and descending order from a given list of integers using streams.
package com.java.streams;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SortingExample {
private static void sortAscending(List<Integer> integerList) {
integerList.stream()
.sorted()
.forEach(System.out::println);
}
private static void sortDescending(List<Integer> integerList) {
integerList.stream()
.sorted(Collections.reverseOrder())
.forEach(System.out::println);
}
public static void main(String[] args) {
System.out.println("Ascending Sort:");
sortAscending(Arrays.asList(49,25,88,88,32,15,10,15,18));
System.out.println("Descending Sort:");
sortDescending(Arrays.asList(49,25,88,88,32,15,10,15,18));
}
}
Output:
Ascending Sort:
10
15
15
18
25
32
49
88
88
Descending Sort:
88
88
49
32
25
18
15
15
10
- Using stream API,square each element in the list and then filter out the numbers greater than 250
package com.java.streams;
import java.util.Arrays;
import java.util.List;
public class SquareUsingMapExample {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(49,25,88,88,32,15,10,15,18);
integerList.stream()
.map(n -> n*n)
.filter(n -> n> 250)
.forEach(System.out::println);
}
}
Output:
2401
625
7744
7744
1024
324
Compute the sum of the first two elements in the provided list of integers using streams.
package com.java.streams; import java.util.Arrays; import java.util.List; public class StreamsLimitExample { public static void main(String[] args) { List<Integer> integerList = Arrays.asList(49, 25, 88, 88, 32, 15, 10, 15, 18); int sum = integerList.stream() .limit(2) .reduce((a, b) -> a + b) .get(); System.out.println("Sum Of First 2 Element Is: " + sum); } } Output:Sum Of First 2 Element Is: 74
After skipping the initial two elements,compute the sum of the remaining elements in the provided list of integers using streams.
package com.java.streams; import java.util.Arrays; import java.util.List; public class StreamsLimitExample { public static void main(String[] args) { List<Integer> integerList = Arrays.asList(49, 25, 88, 88, 32, 15, 10, 15, 18); int sum = integerList.stream() .skip(2) .reduce((a, b) -> a + b) .get(); System.out.println("Sum Of Remaining Element Is: " + sum); } } Output:Sum Of Remaining Element Is: 266
Calculate the sum, minimum, maximum, and average of the first 50 elements using streams
package com.learnJava.numericstreams; import java.util.OptionalDouble; import java.util.OptionalInt; import java.util.OptionalLong; import java.util.stream.IntStream; import java.util.stream.LongStream; public class NumericStreamAggregateExample { public static void main(String[] args) { int sum = IntStream.rangeClosed(1, 50).sum(); System.out.println("Sum : " + sum); OptionalInt max = IntStream.rangeClosed(1, 50).max(); System.out.println("Max : " + max.getAsInt()); OptionalLong min = LongStream.rangeClosed(1, 50).min(); System.out.println("Min : " + min.getAsLong()); OptionalDouble avg = IntStream.rangeClosed(1, 50).average(); System.out.println("Avg : " + avg.getAsDouble()); } } Output: Sum : 1275 Max : 50 Min : 1 Avg : 25.5
Subscribe to my newsletter
Read articles from Sandip Samag directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sandip Samag
Sandip Samag
I am experienced Java Technical Lead with a proven track record of leading and mentoring development teams. Proficient in Java programming, frameworks like Spring, Spring Boot, and Microservices. Skilled in project planning, execution, and agile methodologies. Strong communicator with a focus on delivering high-quality, scalable software solutions.