Java Steam API Flashcards
What is Stream API in Java?
Stream API is a set of tools introduced in Java 8 to help process the sequence of elements. You can use this feature to reduce a large amount of boilerplate code and improve an app’s productivity. It also helps to create more readable programs and allows us to bulk process conveniently.
What is the difference between Collection and Stream?
The main difference between a Collection and a Stream is that Collection contains its elements, but a Stream doesn’t. Stream work on a view where elements are actually stored by Collection or array, but unlike other views, any change made on Stream doesn’t reflect on the original Collection.
What does the map() function do? why you use it?
The map() function perform map functional operation in Java. This means it can transform one type of object to others by applying a function.
For example, if you have a List of String and you want to convert that to a List of Integer, you can use map() to do so.
Just supply a function to convert String to Integer e.g., parseInt() to map() and it will apply that to all elements of List and give you a List of Integer. In other words, the map can convert one object to another.
What does the filter() method do? when you use it?
The filter method is used to filter elements that satisfy a certain condition that is specified using a Predicate function.
A predicate function is nothing but a function that takes an Object and returns a boolean. For example, if you have a List of Integer and you want a list of even integers.
In this case, you can use the filter to achieve that. You supply a function to check if a number is even or odd, just like this function, and filter will apply this to stream elements and filter the elements which satisfy the condition and which don’t.
What does the flatmap() function do? why you need it?
The flatmap function is an extension of the map function. Apart from transforming one object into another, it can also flatten it.
For example, if you have a list of the list but you want to combine all elements of lists into just one list. In this case, you can use flatMap() for flattening. At the same time, you can also transform an object like you do use map() function.
What is the difference between flatMap() and map() functions?
Even though both map() and flatMap() can be used to transform one object to another by applying a method to each element.
The main difference is that flatMap() can also flatten the Stream. For example, if you have a list of the list, then you can convert it to a big list by using the flatMap() function.
What is the difference between intermediate and terminal operations on Stream?
The intermediate Stream operation returns another Stream, which means you can further call other methods of Stream class to compose a pipeline.
For example after calling map() or flatMap() you can still call filter() method on Stream.
On the other hand, the terminal operation produces a result other than Streams like a value or a Collection.
Once a terminal method like forEach() or collect() is called, you cannot call any other method of Stream or reuse the Stream.
What does the peek() method do? When should you use it?
The peek() method of Stream class allows you to see through a Stream pipeline. You can peek through each step and print meaningful messages on the console. It’s generally used for debugging issues related to lambda expression and Stream processing.
What do you mean by saying Stream is lazy?
When we say Stream is lazy, we mean that most of the methods are defined on Java .util.stream.Stream class is lazy. i.e. they will not work by just including them on the Stream pipeline.
They only work when you call a terminal method on the Stream and finish as soon as they find the data they are looking for rather than scanning through the whole set of data.
What is a functional interface in Java 8?
As the name suggests, a functional interface is an interface that represents a function. Technically, an interface with just one abstract method is called a functional interface.
You can also use @FunctionalInterface to annotated a functional interface. In that case, the compiler will verify if the interface actually contains just one abstract method or not. It’s like the @Override annotation, which prevents you from accidental errors.
Another useful thing to know is that If a method accepts a functional interface, then you can pass a lambda expression to it.
Some examples of the functional interface are Runnable, Callable, Comparator, and Comparable from old API and Supplier, Consumer, and Predicate, etc. from new function API.
What is the difference between a normal and functional interface in Java?
The normal interface in Java can contain any number of abstract methods, while the functional interface can only contain just one abstract method.
You might be thinking why they are called functional interfaces? Once you know the answer, it might be a little easier for you to remember the concept.
Well, they are called functional interfaces because they wrap a function as an interface. The function is represented by the single abstract method on the interface.
What is the difference between the findFirst() and findAny() method?
The findFirst() method will return the first element meeting the criterion i.e. Predicate, while the findAny() method will return any element meeting the criterion, very useful while working with a parallel stream. You can further see this article for a working example of the findFirst() method in Java 8.
What is a Predicate interface?
A Predicate is a functional interface that represents a function, which takes an Object and returns a boolean. It is used in several Stream methods like filter(), which uses Predicate to filter unwanted elements.
here is how a Predicate function looks like:
public boolean test(T object) { return boolean; }
You can see it just has one test() method which takes an object and returns a boolean. The method is used to test a condition if it passes; it returns true otherwise false.
What are Supplier and Consumer Functional interface?
The Supplier is a functional interface that returns an object. It’s similar to the factory method or new(), which returns an object.
The Supplier has get() functional method, which doesn’t take any argument and return an object of type T. This means you can use it anytime you need an object.
Since it is a functional interface, you can also use it as the assignment target for a lambda expression or method reference.
A Consumer is also a functional interface in JDK 8, which represents an operation that accepts a single input argument and returns no result.
Unlike other functional interfaces, Consumer is expected to operate via side-effects. The functional method of Consumer is accept(T t), and because it’s a functional interface, you can use it as the assignment target for a lambda expression or method interface in Java 8.
Can you convert an array to Stream? How?
Yes, you can convert an array to Stream in Java. The Stream class provides a factory method to create a Stream from an array, like Stream .of(T …) which accepts a variable argument, that means you can also pass an array to it as shown in the following example:
String[] languages = {"Java", "Python", "JavaScript"}; Stream numbers = Stream.of(languages); numbers.forEach(System.out::println);
Output:
Java
Python
JavaScript