w3resource

Java Stream - Exercises, Practice, Solutions

Java Streams Exercises [ 8 exercises with solution]

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

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. Write a Java program to calculate the average of a list of integers using streams.

Click me to see the solution

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

Click me to see the solution

3. 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. Write a Java program to remove all duplicate elements from a list using streams.

Click me to see the solution

5. 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. 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. 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. 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.