Most Frequently asked Java Streams coding and answers
Omar Ismail
Senior Software Engineer @ Digitinary | Java & Spring Expert ? | AWS & Microservices Architect ? | FinTech & Open Banking Innovator ?? | Digital Payments Expert ?? | Top 200 IT Content Creator in Jordan ?? | 40K+ ??
Input - 1
Assume you are given a list of numbers like {1,7,8,9,5,2,36,4,78,222,24,9}
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
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.
output
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
If we look at the numbers, only?36, 78, 222, 24?squares are greater than?100.
output
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:
output
Odd Numbers
output
5) Given a list of numbers, find out all the numbers starting with 2
Approach: Here we need to do 4 things
output
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
output
duplicates: [9]
Approach 2: Using?Set?to collect only duplicates.
领英推荐
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:
output
Maximum Value: 222
Minimum Value:
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
output
ASC Order: [1, 2, 4, 5, 7, 8, 9, 9, 24, 36, 78, 222]
DESC Order
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
output
First 5 Elements Sum
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.
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/
Senior Software Engineer at Enum Technology Solutions | ASP.NET Web Forms | ASP.NET Core | ASP.NET MVC | Web API | SOAP/Rest WCF Services | Windows Services | Console Applications | AngularJS | SQL | Entity Framework
3 年Abdul Baseer Yawar Ali Khan
Software Developer | Android
3 年Great ! Thanks for sharing
Senior Software Engineer @ EPAM Systems | Java | Spring boot | Microservices | AWS | Docker | DSA | LLD | HLD | Testing
3 年Happy to see my article helping people . Thanks for resharing.