📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Learn Stream API at https://www.javaguides.net/p/java-8-stream-api-tutorial.html
Check out Stream API for beginners at https://www.javaguides.net/2020/04/java-8-stream-tutorial-for-beginners.html
Video Tutorial
1. Stream API Overview
Learn more about Streams at https://www.javaguides.net/p/java-8-stream-api-tutorial.html
2. Characteristics of a Stream
- A stream provides a set of elements of a specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.
- Source − Stream takes Collections, Arrays, or I/O resources as an input source.
- The stream is functional in nature. Operations performed on a stream does not modify its source.
- Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.
3. Different ways to create Stream objects in Java 8
1. Empty Stream
Stream<String> stream = Stream.empty();
stream.forEach(System.out::println);
public Stream<String> streamOf(List<String> list) {
return list == null || list.isEmpty() ? Stream.empty() : list.stream();
}
2. From Collections
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
public class StreamCreationExamples {
public static void main(String[] args) throws IOException {
Collection<String> collection = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
Stream<String> stream2 = collection.stream();
stream2.forEach(System.out::println);
List<String> list = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
Stream<String> stream3 = list.stream();
stream3.forEach(System.out::println);
Set<String> set = new HashSet<>(list);
Stream<String> stream4 = set.stream();
stream4.forEach(System.out::println);
}
}
JAVA
J2EE
Spring
Hibernate
JAVA
J2EE
Spring
Hibernate
JAVA
Hibernate
J2EE
Spring
3. From Arrays
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;
public class StreamCreationExamples {
public static void main(String[] args) throws IOException {
// Array can also be a source of a Stream
Stream<String> streamOfArray = Stream.of("a", "b", "c");
streamOfArray.forEach(System.out::println);
// creating from existing array or of a part of an array:
String[] arr = new String[] { "a", "b", "c" };
Stream<String> streamOfArrayFull = Arrays.stream(arr);
streamOfArrayFull.forEach(System.out::println);
Stream<String> streamOfArrayPart = Arrays.stream(arr, 1, 3);
streamOfArrayPart.forEach(System.out::println);
}
}
a
b
c
a
b
c
b
c
4. From Stream.builder()
Stream<String> streamBuilder = Stream.<String>builder().add("a").add("b").add("c").build();
streamBuilder.forEach(System.out::println);
a
b
c
5. From Stream.generate()
Stream<String> streamGenerated = Stream.generate(() -> "element").limit(10);
streamGenerated.forEach(System.out::println);
element
element
element
element
element
6. From Stream.iterate()
Stream<Integer> streamIterated = Stream.iterate(1, n -> n + 2).limit(5);
streamIterated.forEach(System.out::println);
1
3
5
7
9
7. Stream of File
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class StreamCreationExamples {
public static void main(String[] args) throws IOException {
Path path = Paths.get("C:\\file.txt");
Stream<String> streamOfStrings = Files.lines(path);
Stream<String> streamWithCharset = Files.lines(path, Charset.forName("UTF-8"));
streamOfStrings.forEach(System.out::println);
streamWithCharset.forEach(System.out::println);
streamOfStrings.close();
streamWithCharset.close();
}
}
8. Stream of Primitives
import java.io.IOException;
import java.util.Random;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class StreamCreationExamples {
public static void main(String[] args) throws IOException {
IntStream intStream = IntStream.range(1, 3);
intStream.forEach(System.out::println);
LongStream longStream = LongStream.rangeClosed(1, 3);
longStream.forEach(System.out::println);
Random random = new Random();
DoubleStream doubleStream = random.doubles(3);
doubleStream.forEach(System.out::println);
}
}
1
2
1
2
3
0.6929414814363383
0.3683384343302385
0.948888698350225
Comments
Post a Comment
Leave Comment