java.util.stream
package.
List
, Set
, Array
) that allows
functional operations like filter
, map
,
and sort
, and can run sequentially or in parallel.
filter()
.
map()
(e.g. converting strings to uppercase, objects to another type).
sorted()
.
collect()
.
min()
, max()
,
count()
or anyMatch()
efficiently.
reduce()
(e.g., sum, multiplication).
parallelStream()
for faster performance on multicore systems.
S.No | Class / Interface | Important Methods | Description |
---|---|---|---|
1 | Stream (Interface) |
filter() , map() , distinct() ,
sorted() , limit() , skip() , forEach()
|
Represents a sequence of elements supporting sequential and parallel aggregate operations. |
2 | IntStream (Interface) |
range() , rangeClosed() , sum() ,
average() , min() , max()
|
Specialized stream for handling int values efficiently. |
3 | LongStream (Interface) |
range() , rangeClosed() , sum() ,
average() , min() , max()
|
Specialized stream for handling long values efficiently. |
4 | DoubleStream (Interface) |
of() , average() , sum() ,
min() , max() , summaryStatistics()
|
Specialized stream for handling double values efficiently. |
5 | Collectors (Class) |
toList() , toSet() , toMap() ,
joining() , groupingBy() , partitioningBy()
|
Provides static methods to accumulate elements from a stream into collections or other forms. |
6 | Optional (Class) |
isPresent() , get() , orElse() ,
ifPresent() , map() , flatMap()
|
Represents a container object which may or may not contain a non-null value, used for safe handling of nulls. |
7 | BaseStream (Interface) |
iterator() , spliterator() , close() ,
parallel() , sequential()
|
Parent interface for all streams providing common operations like closing and switching between parallel/sequential. |
List
, Set
, or arrays.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
.stream()
or Arrays.stream()
.
Stream<Integer> stream = numbers.stream();
stream = stream
.filter(n -> n % 2 == 0) // keep even numbers
.map(n -> n * n); // square them
filter()
, map()
, sorted()
,
distinct()
, limit()
stream.forEach(System.out::println); // prints result
collect()
, forEach()
, reduce()
,
count()
, toArray()
import java.util.List;
import java.util.Arrays;
public class MainApp1
{
public static void main(String[] args)
{
// 1️⃣ DATA SOURCE
List numbers = Arrays.asList(1, 2, 3, 4, 5);
// 2️⃣ CREATE STREAM from the data source
numbers.stream()
// 3️⃣ INTERMEDIATE OPERATIONS (lazy)
.filter(n -> n % 2 == 0) // filter: keep only even numbers
.map(n -> n * n) // map: square the even numbers
// 4️⃣ TERMINAL OPERATION (triggers execution)
.forEach(System.out::println); // print each result
// 5️⃣ OUTPUT
// 4 (2 * 2)
// 16 (4 * 4)
}
}
4 16
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
public class MainApp2
{
public static void main(String[] args)
{
List names = Arrays.asList("diksha", "rahul", "amit", "deepesh", "neha", "priya", "deepak");
List result = names.stream()
.filter(name -> name.startsWith("d"))
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
System.out.println(result);
}
}
[DEEPAK, DEEPESH, DIKSHA]
for
or while
loops).
filter()
, map()
, sorted()
for better readability.
collect()
or forEach()
) is called.
parallelStream()
to take advantage of multi-core processors.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.