The Java Stream API, introduced in Java 8, allows functional-style operations on collections, differing from InputStream and OutputStream which are used for I/O operations. Key operations include intermediate operations (like filter and map) and terminal operations (like collect and forEach). Streams are lazy, cannot be reused after a terminal operation, and support parallel processing, enhancing data processing efficiency and readability.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
24 views
Stream interview questions
The Java Stream API, introduced in Java 8, allows functional-style operations on collections, differing from InputStream and OutputStream which are used for I/O operations. Key operations include intermediate operations (like filter and map) and terminal operations (like collect and forEach). Streams are lazy, cannot be reused after a terminal operation, and support parallel processing, enhancing data processing efficiency and readability.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14
What is the Java Stream API?
How is it different from InputStream
and OutputStream? Answer: The Stream API is part of the java.util.stream package introduced in Java 8 for functional-style operations on collections. It is different from InputStream and OutputStream as those are used for I/O operations. What are the main types of operations available in the Stream API? Answer: Intermediate operations: transform or filter a stream and return another stream as a result (e.g., filter, map, sorted). Terminal operations: Produce a result or side effect (e.g., forEach, collect, count). Explain the difference between map and flatMap. Answer: map :transforms each element in the stream into another value. Produces a one-to-one mapping, where each input element results in a single output element. flatMap: transforms each element into a stream and then flattens all the resulting streams into one. Produces a one-to-many mapping, where each input element can result in multiple output elements. Explain the difference between map and flatMap- example What is the difference between stream() and parallelStream()? Answer: stream(): creates a sequential stream that processes elements one by one. parallelStream(): creates a parallel stream that divides the tasks among multiple threads for concurrent processing. What is the difference between stream() and parallelStream()? Answer: stream(): creates a sequential stream that processes elements one by one. parallelStream(): creates a parallel stream that divides the tasks among multiple threads for concurrent processing.
What does it mean that streams are lazy?
Answer: Streams only process data when a terminal operation is invoked. Intermediate operations are not executed immediately. Can a stream be reused? Why or why not? Answer: No, a stream cannot be reused once a terminal operation is invoked because the stream is considered consumed. You must create a new stream.
What is the difference between findFirst() and findAny()?
Answer: findFirst() returns the first element in the stream. findAny() returns any element (useful for parallel streams where order doesn’t matter). What are the benefits of using the Stream API? Answer: Simplified data processing using functional programming. Improved readability and reduced boilerplate code. Support for parallel processing with parallelStream().
How does parallel stream work internally?
Answer: Parallel streams use the ForkJoinPool framework to divide the data into chunks and process them concurrently using multiple threads. What is a Collector in the Stream API? Answer: A Collector is an interface used in the collect() terminal operation to define how to accumulate the elements of a stream into a collection, map, or other container.
How can you handle exceptions in streams?
Answer: You can wrap the lambda expressions with a try-catch block or create a utility method to handle checked exceptions. What is a Collector in the Stream API? Answer: A Collector is an interface used in the collect() terminal operation to define how to accumulate the elements of a stream into a collection, map, or other container.
What is the purpose of the peek() method?
Answer: peek() is used for debugging or logging by performing a side effect on elements without modifying the stream. What is the difference between reduce() and collect()? Answer: reduce() is used to combine elements of a stream into a single value (e.g., sum, average). - int sum = numbers.stream() .reduce(0, Integer::sum); collect() is used to transform elements into a collection or container. What are the differences between limit() and skip()? Answer: limit(n): Limits the stream to the first n elements. skip(n): Skips the first n elements and processes the rest.
How can you convert a stream into a collection or array?
Answer: Use collect(Collectors.toList()) for collections. Use toArray() for arrays. What are infinite streams? Provide an example. Answer: Infinite streams are streams that do not have a fixed size and continue to produce elements indefinitely. Example: What are infinite streams? Provide an example. Answer: Infinite streams are streams that do not have a fixed size and continue to produce elements indefinitely. Example: