w3resource

Java Stream - Exercises, Practice, Solutions


This resource offers a total of 40 Java Streams problems for practice. It includes 8 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]

Processing Data with Java SE 8 Streams:

Stream is a sequence of elements from a source that supports aggregate operations. Let’s break it down:

  • Sequence of elements: A stream provides an interface to a sequenced set of values of a specific element type. However, streams don’t actually store elements; they are computed on demand.
  • Source: Streams consume from a data-providing source such as collections, arrays, or I/O resources.
  • Aggregate operations: Streams support SQL-like operations and common operations from functional programing languages, such as filter, map, reduce, find, match, sorted, and so on.

1. Calculate average of integers using streams

Write a Java program to calculate the average of a list of integers using streams.

Click me to see the solution

2. Convert strings to upper/lowercase using streams

Write a Java program to convert a list of strings to uppercase or lowercase using streams.

Click me to see the solution

3. Sum even and odd numbers in list using streams

Write a Java program to calculate the sum of all even, odd numbers in a list using streams.

Click me to see the solution

4. Remove duplicates from list using streams

Write a Java program to remove all duplicate elements from a list using streams.

Click me to see the solution

5. Count strings starting with letter using streams

Write a Java program to count the number of strings in a list that start with a specific letter using streams.

Click me to see the solution

6. Sort strings A-Z and Z-A using streams

Write a Java program to sort a list of strings in alphabetical order, ascending and descending using streams.

Click me to see the solution

7. Find max and min in list using streams

Write a Java program to find the maximum and minimum values in a list of integers using streams.

Click me to see the solution

8. Find 2nd smallest/largest using streams

Write a Java program to find the second smallest and largest elements in a list of integers using streams.

Click me to see the solution

Java Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.