0% found this document useful (0 votes)
4 views7 pages

5 Tricky Java Stream Problems For Interviews 1708367144

The document presents five Java Stream problems commonly encountered in interviews. It includes tasks such as filtering even numbers to find their sum, calculating the average of a list of doubles, counting character occurrences in a string, sorting a list of strings by length, and checking for duplicates in a list of integers. Each problem is accompanied by sample code demonstrating the solution using Java Streams.

Uploaded by

Sunil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

5 Tricky Java Stream Problems For Interviews 1708367144

The document presents five Java Stream problems commonly encountered in interviews. It includes tasks such as filtering even numbers to find their sum, calculating the average of a list of doubles, counting character occurrences in a string, sorting a list of strings by length, and checking for duplicates in a list of integers. Each problem is accompanied by sample code demonstrating the solution using Java Streams.

Uploaded by

Sunil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

5 Tricky Java Stream

Problems for Interviews


Given a list of integers, filter out the even
numbers and return their sum
• List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
• int sumOfEvenNumbers = numbers.stream()
.filter(num -> num % 2 == 0)
.mapToInt(Integer::intValue).sum();
Given a list of doubles, find the average
• List<Double> doubles = Arrays.asList(2.5, 3.6, 4.8, 1.2, 7.4);
• OptionalDouble average = doubles.stream()
.mapToDouble(Double::doubleValue).average();
Given a string, count occurrences of a
specific character
• String text = "hello world“;
• long count = text.chars().filter(ch -> ch == 'o').count();
Given a list of strings, sort them by length in
ascending order
• List<String> strings = Arrays.asList("apple", "banana", "orange",
"kiwi", "grape");
• List<String> sortedByLength = strings.stream()
.sorted(Comparator.comparingInt(String::length))
.collect(Collectors.toList());
Given a list of integers, check if it contains
any duplicate elements
• List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 2, 7, 8, 9, 10);
• boolean hasDuplicates = numbers.stream().distinct() .count() !=
numbers.size();
Congratulations !!

You might also like