Last Updated : 11 Jul, 2025
The
Stream API, introduced in Java 8, it is used to process collections of objects. Stream is a sequence of objects, that supports many different methods which can be pipe lined to produce the desired result. The features of Java stream are –
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(List<T> list)
{
// Create stream object with the List
Stream<T> stream = list.stream();
// Iterate list first to last element
Iterator<T> it = stream.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Create ArrayList of String
List<String> list = new ArrayList<>();
// Add element in list
list.add("Geeks");
list.add("for");
list.add("Geeks");
// Get the Stream from the List
getStream(list);
}
}
// Java program to create Stream from values
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static void getStream()
{
// Create a stream from specified values
Stream<Integer> stream
= Stream.of(1, 2,
3, 4,
5, 6,
7, 8,
9);
// Displaying the sequential ordered stream
stream.forEach(p -> System.out.print(p + " "));
}
public static void main(String[] args)
{
// Get the Stream from the values
getStream();
}
}
Output:
1 2 3 4 5 6 7 8 9
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(T[] arr)
{
// Create stream from an array
// using Arrays.stream()
Stream<T> streamOfArray
= Arrays.stream(arr);
// Iterate list first to last element
Iterator<T> it
= streamOfArray.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the array
String[] arr
= new String[] { "a", "b", "c" };
// Get the Stream from the Array
getStream(arr);
}
}
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(T[] arr)
{
// Create stream from an array
// using Stream.of()
Stream<T> streamOfArray = Stream.of(arr);
// Iterate list first to last element
Iterator<T> it = streamOfArray.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the array
String[] arr
= new String[] { "a", "b", "c" };
// Get the Stream from the Array
getStream(arr);
}
}
// Java program to create empty Stream
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static void getStream()
{
// Create stream from an array using Stream.empty()
Stream<String> streamOfArray
= Stream.empty();
// Iterate list first to last element
Iterator<String> it
= streamOfArray.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the empty Stream
getStream();
}
}
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static <T> void getStream()
{
// Create stream using Stream builder()
Stream.Builder<String> builder
= Stream.builder();
// Adding elements in the stream of Strings
Stream<String> stream = builder.add("a")
.add("b")
.add("c")
.build();
// Iterate list first to last element
Iterator<String> it = stream.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the Stream using Builder
getStream();
}
}
// Java program to create infinite Stream
// using Stream.iterate() method
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Function convert a List into Stream
private static <T> void
getStream(int seedValue, int limitTerms)
{
// Create infinite stream
// using Stream.iterate() method
Stream.iterate(seedValue,
(Integer n) -> n * n)
.limit(limitTerms)
.forEach(System.out::println);
}
public static void main(String[] args)
{
// Get the seed value
int seedValue = 2;
// Get the limit for number of terms
int limitTerms = 5;
// Get the Stream from the function
getStream(seedValue, limitTerms);
}
}
// Java program to create infinite Stream
// using Stream.generate() method
import java.util.*;
import java.util.stream.*;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(int limitTerms)
{
// Create infinite stream
// using Stream.generate() method
Stream.generate(Math::random)
.limit(limitTerms)
.forEach(System.out::println);
}
public static void main(String[] args)
{
// Get the limit for number of terms
int limitTerms = 5;
// Get the Stream from the function
getStream(limitTerms);
}
}
Output:
0.2293502475696314 0.5650334795948209 0.3418138293253522 0.36831074763500116 0.4864408670097241
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.*;
import java.util.regex.Pattern;
class GFG {
// Function convert a List into Stream
private static void
getStream(List<String> list, Pattern p)
{
list.stream()
.filter(p.asPredicate())
.forEach(System.out::println);
}
public static void main(String[] args)
{
// Create ArrayList of String
// that is backed by the specified array
List<String> list
= Arrays
.asList("Geeks",
"For",
"Geek",
"GeeksForGeeks",
"A Computer Portal");
// Get the pattern
Pattern p = Pattern.compile("^G");
// Get the Stream from the List matching Pattern
getStream(list, p);
}
}
Output:
Geeks Geek GeeksForGeeks
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.*;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(Iterator<T> itr)
{
// Convert the iterator into a Spliterator
Spliterator<T> spitr
= Spliterators
.spliteratorUnknownSize(itr,
Spliterator.NONNULL);
// Convert spliterator into a sequential stream
Stream<T> stream
= StreamSupport.stream(spitr, false);
// Iterate list first to last element
Iterator<T> it = stream.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the Iterator
Iterator<String> iterator = Arrays
.asList("a", "b", "c")
.iterator();
// Get the Stream from the Iterator
getStream(iterator);
}
}
// Java program to create Stream from Collections
import java.util.*;
import java.util.stream.*;
class GFG {
// Function convert a List into Stream
private static <T> void getStream(Iterable<T> iterable)
{
// Convert the iterator into a Stream
Stream<T> stream
= StreamSupport
.stream(iterable.spliterator(),
false);
// Iterate list first to last element
Iterator<T> it = stream.iterator();
// Iterate stream object
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args)
{
// Get the Iterable
Iterable<String> iterable
= Arrays.asList("a", "b", "c");
// Get the Stream from the Iterable
getStream(iterable);
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4