Java Stream API

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/54

flashcard set

Earn XP

Description and Tags

Java

Stream

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

55 Terms

1
New cards

What is the Java Stream API?

A feature introduced in Java 8 that allows processing sequences of elements using functional-style operations.

2
New cards

Which package contains the Stream API?

java.util.stream

3
New cards

What is a stream in Java?

A sequence of elements that supports sequential and parallel aggregate operations.

4
New cards

Is a stream a data structure?

No, it's not a data structure; it doesn't store data but operates on data from a source.

5
New cards

What are the main types of streams in Java?

Stream<T>, IntStream, LongStream, and DoubleStream

6
New cards

What is the difference between intermediate and terminal operations in streams?

Intermediate operations return a new stream; terminal operations produce a result or side effect and terminate the stream.

7
New cards

Give examples of intermediate operations.

map(), filter(), sorted(), distinct(), limit(), skip()

8
New cards

Give examples of terminal operations.

collect(), forEach(), reduce(), count(), anyMatch(), allMatch()

9
New cards

What is the purpose of filter()?

Filters elements based on a given predicate.

10
New cards

What does map() do in streams?

Transforms each element in the stream using a function.

11
New cards

What is flatMap() used for?

Flattens nested structures into a single stream.

<p>Flattens nested structures into a single stream.</p>
12
New cards

What is collect() used for?

Performs a mutable reduction on the elements and returns a collection or result.

<p>Performs a mutable reduction on the elements and returns a collection or result.</p>
13
New cards

What is Collectors.toList()?

A collector that accumulates the stream elements into a List.

14
New cards

What does forEach() do?

Performs an action for each element in the stream.

15
New cards

What is reduce() in streams?

Combines elements of the stream into a single result using an associative accumulation function.

<p>Combines elements of the stream into a single result using an associative accumulation function.</p>
16
New cards

What does sorted() do in streams?

Sorts elements based on natural order or a provided comparator.

<p>Sorts elements based on natural order or a provided comparator.</p>
17
New cards

How do you create a stream from a collection?

Using collection.stream() or collection.parallelStream()

18
New cards

How do you create a stream from an array?

Using Arrays.stream(array)

19
New cards

What is the difference between Stream.of() and Arrays.stream()?

Stream.of() creates a stream from varargs or single values; Arrays.stream() specifically works with arrays.

20
New cards

What is a parallel stream?

A stream that uses multiple threads to process data concurrently.

21
New cards

How do you create a parallel stream?

Using collection.parallelStream() or stream.parallel()

22
New cards

What is limit(n) in streams?

Truncates the stream to be no longer than n elements.

<p>Truncates the stream to be no longer than <code>n</code> elements.</p>
23
New cards

What is skip(n) in streams?

Skips the first n elements of the stream.

<p>Skips the first <code>n</code> elements of the stream.</p>
24
New cards

What is distinct() in streams?

Removes duplicate elements from the stream.

<p>Removes duplicate elements from the stream.</p>
25
New cards

What does peek() do in streams?

Provides a way to see the elements as they flow through the stream, mainly for debugging.

<p>Provides a way to see the elements as they flow through the stream, mainly for debugging.</p>
26
New cards

Are streams reusable in Java?

No, streams can be consumed only once.

27
New cards

What happens if you try to reuse a stream?

An IllegalStateException is thrown.

28
New cards

What is lazy evaluation in streams?

Intermediate operations are not executed until a terminal operation is invoked.

29
New cards

What is short-circuiting in streams?

The ability to terminate stream processing early when a result is found (e.g., anyMatch()).

30
New cards

What is anyMatch()?

Returns true if any element in the stream matches the given predicate.

31
New cards

What is allMatch()?

Returns true if all elements match the predicate.

<p>Returns true if all elements match the predicate.</p>
32
New cards

What is noneMatch()?

Returns true if no elements match the predicate.

33
New cards

What does findFirst() do?

Returns an Optional describing the first element of the stream.

<p>Returns an <code>Optional</code> describing the first element of the stream.</p>
34
New cards

What does findAny() do?

Returns an Optional describing any element of the stream (useful for parallel streams).

<p>Returns an <code>Optional</code> describing any element of the stream (useful for parallel streams).</p>
35
New cards

What is Optional in Java?

A container object which may or may not contain a non-null value.

36
New cards

What is the role of Stream.generate()?

Creates an infinite stream using a Supplier.

<p>Creates an infinite stream using a Supplier.</p>
37
New cards

What is the role of Stream.iterate()?

Creates an infinite sequential ordered stream from a seed and a function.

<p>Creates an infinite sequential ordered stream from a seed and a function.</p>
38
New cards

How do you limit infinite streams?

By using limit(n) to avoid infinite loops.

39
New cards

How do you convert a stream to an array?

Using stream.toArray()

<p>Using <code>stream.toArray()</code></p>
40
New cards

What is Collectors.groupingBy() used for?

To group elements by a classifier function.

<p>To group elements by a classifier function.</p>
41
New cards

What is Collectors.partitioningBy()?

Divides elements into two groups based on a predicate.

<p>Divides elements into two groups based on a predicate.</p>
42
New cards

What is the difference between map() and flatMap()?

map() transforms each element; flatMap() flattens nested structures.

43
New cards

Can you perform file I/O using streams?

Yes, using Files.lines(Path) returns a Stream<String> of lines from a file.

<p>Yes, using <code>Files.lines(Path)</code> returns a <code>Stream&lt;String&gt;</code> of lines from a file.</p>
44
New cards

What is IntStream.range()?

Creates a sequential stream of int values in a given range.

<p>Creates a sequential stream of int values in a given range.</p>
45
New cards

What is Stream.concat()?

Combines two streams into one.

<p>Combines two streams into one.</p>
46
New cards

How do you sum values in a stream?

Using mapToInt().sum() or reduce()

<p>Using <code>mapToInt().sum()</code> or <code>reduce()</code></p>
47
New cards

What is Collectors.joining() used for?

Concatenates the elements of a stream into a single String.

<p>Concatenates the elements of a stream into a single <code>String</code>.</p>
48
New cards

How do you convert a stream to a map?

Using Collectors.toMap()

<p>Using <code>Collectors.toMap()</code></p>
49
New cards

What does Stream.builder() do?

Allows step-by-step construction of a stream.

<p>Allows step-by-step construction of a stream.</p>
50
New cards

What is the advantage of Stream API?

Improves code readability, encourages functional programming, and enables parallel processing.

51
New cards

Can exceptions be thrown inside streams?

Yes, but they must be handled (e.g., wrapped in try-catch or use custom utilities).

52
New cards

What happens if a stream pipeline throws an exception?

It terminates the pipeline unless the exception is caught.

53
New cards

What is Stream.empty()?

Creates an empty stream.

54
New cards

What is BaseStream?

A common interface for all primitive and object streams.

55
New cards

What are primitive specializations in streams?

IntStream, LongStream, and DoubleStream for better performance with primitives.