Most Frequently asked Java Streams coding and answers

Most Frequently asked Java Streams coding and answers

Input - 1

Assume you are given a list of numbers like {1,7,8,9,5,2,36,4,78,222,24,9}

No alt text provided for this image

Answers

1) Given a list of numbers, return the sum of all numbers.

Approach: We can use reduce() method whenever we want to produce a single resultant value as output, for example, maximum, minimum, sum, product, etc.

You can refer Stream.reduce for more details.

Optional<Integer> sum = list.stream().reduce((a, b) -> a+b);
System.out.println("sum is: "+sum.get());        

output

No alt text provided for this image

2) Given a list of numbers, return the average of all numbers

Approach: We can use mapToInt() followed by average() whenever we want to perform an average on the list of integers.

No alt text provided for this image

output

No alt text provided for this image

3) Given a list of numbers, square them and filter the numbers which are greater 100 and then find the average of them

Approach: Here we need to do 3 things

  • We need to square each number ( We can use map())
  • Filter whose value is greater than 100 (we can use filter())
  • Find average of those (we can use mapToInt() and average() together)

No alt text provided for this image

If we look at the numbers, only 36, 78, 222, 24 squares are greater than 100.

  • (1296 + 6084 + 49284 + 576)/4 = 14310.0

output

No alt text provided for this image

4) Given a list of numbers, return the even and odd numbers separately

Approach: We can use filter() and Collectors.toList() to get both even and odd numbers as two separate lists.

Even Numbers:

No alt text provided for this image

output

No alt text provided for this image

Odd Numbers

No alt text provided for this image

output

No alt text provided for this image

5) Given a list of numbers, find out all the numbers starting with 2

Approach: Here we need to do 4 things

  • Convert Integer to String to perform startsWith operation on it
  • Filter the strings that starts with 2
  • Convert String to Integer on filtered data
  • Collect the final Integers as List and store

No alt text provided for this image

output

No alt text provided for this image

6) Given a list of numbers, print the duplicate numbers

Approach 1: Using frequency() method of Collections class. It counts the frequency of the specified element in the given list. If count > 1 then that element is duplicate one

No alt text provided for this image

output

duplicates: [9]        

Approach 2: Using Set to collect only duplicates.

No alt text provided for this image

output

duplicates: [9]        

7) Given a list of numbers, print the maximum and minimum values

Approach: Using max() and min() we can get maximum and minimum values from a list along with Comparator.comparing().

Maximum Value:

No alt text provided for this image

output

Maximum Value: 222        

Minimum Value:

No alt text provided for this image

output

Minimum Value: 1        

8) Given a list of numbers, sort them in ASC and DESC order and print

Approach: Using sorted(), We can sort a list in ASC or DESC order.

ASC Order

No alt text provided for this image

output

ASC Order: [1, 2, 4, 5, 7, 8, 9, 9, 24, 36, 78, 222]        


DESC Order

No alt text provided for this image

output

DESC Order: [222, 78, 36, 24, 9, 9, 8, 7, 5, 4, 2, 1]        

9) Given a list of numbers, return the first 5 elements and their sum

Approach: We can use limit() followed by reduce().

First 5 Elements

No alt text provided for this image

output

No alt text provided for this image

First 5 Elements Sum

No alt text provided for this image

output

first5elementsSum: 30        

10) Given a list of numbers, skip the first 5 numbers and return the sum of the remaining numbers

Approach: We can use skip() to skip the first n numbers in a list.

No alt text provided for this image

output

Sum after first 5 elements skip: 375        

11) Given a list of numbers, return the cube of each number

Approach: We can use map() here.

List<Integer> cubes = list.stream().map(num -> num*num*num).collect(Collectors.toList());
System.out.println("Cubes: "+cubes);        

output

Cubes: [1, 343, 512, 729, 125, 8, 46656, 64, 474552, 10941048, 13824, 729]        


Thanks To: shivaprasadgurram.hashnode.dev/

To view or add a comment, sign in

More articles by Omar Ismail

Others also viewed

Explore content categories