click below
click below
Normal Size Small Size show me how
Java Stream
Question | Answer |
---|---|
Stream takes Collection, Arrays, or I/O resources as input source | true |
Which package should be imported to use Stream API in Java 8? | java.util.stream includes all the classes and interfaces used for functional-type operations |
<> terminal operation is used to convert the source stream into collections by using intermediate operations | Collect() Result stream may be of the list, set, map, etc |
Which are intermediate operations in Stream API? | filter map sorted |
How many operations are performed in stream API to get the result? | two Intermediate( will process the stream to get the result and terminal operations (it is the end of the stream to return the result) |
< > will work on the streams and transform the single input value into multiple outputs by flattening it | flatMap() |
choose all that apply, forEach() is | used when we need to iterate the elements in the stream is a terminal operation that returns void can be called directly collections or stream |
main() { Stream.iterate(1, element->element+1) .filter(element->element%2==0) .limit(4) forEach(System.out::println); } | 2,4,6,8 iterate() is used to iterate through the elements in the stream, filter() used to apply the condition on the stream and forEach() is used to return the result from the stream after iteration |
Stream<String> s = Stream.of("apple", "orange", "apple", "banana", "banana"); s.distinct().forEach(System.out::print); | returns appleorangebanana distinct() will return a stream from the source stream removing the duplicate elements |
< > is a collection of objects which can be processed to get the desired result | Stream API |
< > terminal operations in stream API is used to return the result of the stream in a finite numbers | count() ex. Which returns the number of employees working in a particular department |
Which are ways to create a stream in Java8? | using Stream.of() method using stream() & parallelStream() methods using Arrays.stream() |
Stream<String> s = Stream.of("java", "SQL", "python", "JDBC"); s.filter(x->x.startsWith("S")).forEach(System.out::print); | returns SQL Here we are using the filter to return the result of the element starting with "S" |
Can we add or delete elements from streams? | false we can only perform the operations on the stream and streams does not store the data as well |
We need to import java.util.stream even after importing java.util.* in the code to use stream API | True Stream class resides in the sub package java.util.stream, so it will not be included in java.util.* |
Will the value of elements in the stream change when you process it? | false Because stream API processes the elements as per pipelined operations without changing the values |
< > can be used when we need to return the stream of elements in sorted order like sorting arrays | sorted() |
Which is a terminal operations from the following in Stream API | forEach |
Marshalling is… | The process of sending data to a destination the process of converting an object instance into a data format that describes it |
Reflection | Allows Runtime modification of objects |
Which of the following is an example of a marker interface? | Serializable |
Lambda is used to implement a functional Interface( an interface with a single abstract method) | true |
Marker interfaces are used to pass the information to JVM that a certain object of a class can implement methods like Serializable, Cloneable etc. | true |
1. Local class 2. Anonymous class 3. Lambda Expression 4. Nested class 4-a. Used to group classes that are reliable with each other, to increase encapsulation and to maintain cohesive, concise and readable code | 1-d. Used to create more than one instance of a class, and to add new fields and methods to the class 2-c. Used when you need to use a local class only once 3-b. Used to create a simple instance of a functional interface |