Interview Questions
Java
Java Interview Questions
Java interview questions
Java Interview Questions
java8
Java8 Examples
java8 streams examples
Java8 Streams features and Stream creation examples
Java streams are a powerful feature introduced in Java 8 that allow for functional-style programming and operations on data in collections. Some key features of Java streams include:
- Laziness: Stream operations are not executed until a terminal operation (such as forEach or reduce) is called. This allows for operations to be chained together and executed only when necessary.
- Pipelining: Stream operations are performed in a “pipeline” fashion, with the output of one operation becoming the input of the next.
- Immutability: Streams do not modify the underlying data, but instead return new streams with the result of the operation.
- Functional in nature
- Consumable
- No Intermediate Store
Multiple ways to create Streams:
Java 8 streams can be created from various sources, such as collections, arrays, and I/O channels. Here are some examples of creating streams in Java 8:
- Creating a stream from a collection:
Using the stream()
method: This method is available on the Collection interface, so you can use it to convert any Collection (such as List, Set, Queue) to a Stream.
List<String> words = Arrays.asList("Java", "Streams", "are", "powerful");
Stream<String> stream = words.stream();
- Using Stream of:
This method is available on the Stream class and it can create a stream from any number of elements.
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
- Using the
iterate()
method: This method is available on the Stream class and it creates an infinite stream of elements by applying the given function to the previous element.
Stream<Integer> naturalNumbers = Stream.iterate(0, n -> n + 1);
- Creating a stream from an array:
int[] numbers = {1, 2, 3, 4, 5};
IntStream intStream = Arrays.stream(numbers);
- Using the
generate()
method: This method is also available on the Stream class and it creates an infinite stream of elements by applying the given supplier function.
Stream<String> randomStrings = Stream.generate(() -> UUID.randomUUID().toString());
- Using the
parallelStream()
method: This method is available on the Collection interface, it creates a parallel stream of elements, which can be processed in parallel using multiple threads.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> parallelNumberStream = numbers.parallelStream();
- Creating a stream from a file:
try (Stream<String> lines = Files.lines(Paths.get("file.txt"), StandardCharsets.UTF_8)) {
lines.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
- Creating an empty stream
Stream<String> emptyStream = Stream.empty();
- Creating a stream of a specific size with a specific value
Stream<Integer> stream = Stream.generate(() -> 2).limit(5);
- Creating a stream with a range of values
IntStream intStream = IntStream.range(1, 5);
- Using Buffer Reader
BufferedReader.lines()
- Zip and Jar File
ZipFile.stream()
JarFile.stream()
Comments are closed.