Functional Programming in Java 8+ using the Stream API with Example
Last Updated :
09 Dec, 2021
API is an acronym for Application Programming Interface, which is software and the java streams work on a data source. Consider a stream like a flow of water in a small canal. Let’s take a real-life example. Each time a user uses an application that is popular these days like WhatsApp in order to communicate via delivering text messages or calls to other users. Both users are using an API.
Java streams work on three operations which as mentioned below
- Data Source
- Intermediate operation
- Terminal operation
Methods: Streams can be created in three ways
- Using an object of any class from the collection framework
- Using an array of the reference data type
- Using the interface defined in the ‘java.util.stream’ package
Method 1: Data Source
The data source can be widely varied such as an array, List, etc
Syntax:
ArrayList<Integer> numbers = new ArrayList<>();
Integer[] numbers = {1,2,3};
Example 1: Using an object as a data source
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> gfgNames = new ArrayList<>();
gfgNames.add( "Dean" );
gfgNames.add( "castee" );
gfgNames.add( "robert" );
Stream<String> streamOfNames = gfgNames.stream();
System.out.print(streamOfNames);
}
}
|
Example 2: Using an array as a data source
// Data Source
Integer[] numbers = {1,2,3,4,5};
// Stream using an array
Stream<Integer> streamOfNumbers = Arrays.stream(numbers);
// using predefined Instream interface
integerStream = IntStream.range(1,100); // a stream from 1 to 99;
Java
import java.io.*;
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
var stream = IntStream.range( 1 , 100 );
var max = stream.filter(number -> number % 4 == 0 )
.count();
System.out.println(max);
}
}
|
24
Method 2: Intermediate Operation
Intermediate operations are some methods that one can apply on a stream.
Note: It can be of any number
filter()
Example:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
Integer[] numbers = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 }
Stream<Integer> streamOfNumbers
= Arrays.stream(numbers);
Stream<Integer> evenNumbersStream
= streamOfNumbers.filter(
number -> number % 2 == 0 )
}
}
|
Method 3: Terminal operation
Terminal operation methods that we can apply on a stream that will cause a stream to be “closed”.
Concept:
Some terminal operations can be used to iterate on the elements of the stream.
min(),max(),count()
forEach(),noneMatch()
Example 1: Explaining stream API
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Integer[] numbers
= { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 };
var streamOfNumbers = Arrays.stream(numbers);
var evenNumbersStream
= streamOfNumbers
.filter(number -> number % 3 == 0 )
.count();
System.out.println(evenNumbersStream);
}
}
|
Example 2:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add( 20 );
list.add( 4 );
list.add( 76 );
list.add( 21 );
list.add( 3 );
list.add( 80 );
var stream = list.stream();
var numbers
= stream.filter(number -> number % 2 == 0 )
.filter(number -> number > 20 );
numbers.forEach(System.out::println);
}
}
|
Note: One can pass lambda also number -> System.out.println(number + ” “)
Share your thoughts in the comments
Please Login to comment...