Interview Questions & Answers

java

The intermediate Stream operation returns another Stream, which means you can further call other methods of Stream class to compose a pipeline.
Intermediate Stream operations:
  • filter()
  • map()
  • flatMap()
  • distinct()
  • sorted()
  • peek()
  • limit()
  • skip()

For example, after calling filter() you can still call the map() method on Stream.

// Converting product List into Set
        Set < Float > productPriceList = productsList.stream().filter(product -> product.getPrice() < 30000)
        .map(product -> product.getPrice()).collect(Collectors.toSet());
Stream terminal operation produces a result other than Streams like a value or a Collection.
Terminal Stream operations:
  • anyMatch()
  • allMatch()
  • noneMatch()
  • collect()
  • count()
  • findAny()
  • findFirst()
  • forEach()
  • min()
  • max()
  • reduce()
  • toArray()

Once a terminal method like forEach() or collect() is called, you cannot call any other method of Stream or reuse the Stream.”

List<String> lines = Arrays.asList("java", "c", "python");

List<String> result = lines.stream()       // convert list to stream
.filter(line -> !"c".equals(line)) // we dont like c
.collect(Collectors.toList());     // collect the output and convert streams to a List

result.forEach(System.out::println);

The key difference between map() vs flatMap() in Java 8:

  • The function you pass to the map() operation returns a single value.
  • The function you pass to flatMap() operation returns a Stream of value.
  • flatMap() is a combination of map and flat operation.
  • map() is used for transformation only, but faltMap() is used for both transformation and flattening.

The Stream.flatMap() function, as the name suggests, is the combination of a map and a flat operation. This means you first apply the map function and then flatten the result.
To understand what flattening a stream consists in, consider a structure like [ [1,2,3],[4,5,6],[7,8,9] ] which has “two levels”. It’s basically a big List containing three more List. Flattening this means transforming it in a “one level” structure e.g. [ 1,2,3,4,5,6,7,8,9 ] i.e. just one list.
For example: In the below program, you can see that we have three lists that are merged into one by using a flatMap() function.
public class Main {
    public static void main(String[] args)
    {
        List<Integer> evens = Arrays.asList(2, 4, 6);
        List<Integer> odds = Arrays.asList(3, 5, 7);
        List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11);
        List<Integer> numbers = Stream.of(evens, odds, primes)
                .flatMap(list -> list.stream())
                .collect(Collectors.toList());
        System.out.println("flattend list: " + numbers);
    }
}

Output:
flattend list: [2, 4, 6, 3, 5, 7, 2, 3, 5, 7, 11]

The map() function is an intermediate function that is used to perform map functional operations in Java. This means it can transform one type of object to others by applying a function.
Use map() function to convert one object to another object.
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.

        import java.util.Arrays;
        import java.util.List;
        import java.util.stream.Collectors;

public class Main
{
    public static void main(String[] args)
    {
        List<String> listOfStrings = Arrays.asList("1", "2", "3", "4", "5");

        List<Integer> listOfIntegers = listOfStrings.stream()
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        System.out.println(listOfIntegers);
    }
}

output:
java
python



1. Collections are used to store and group the data in a particular data structure like List, Set, or Map. Whereas Streams are used to perform complex data processing operations like filteringmatchingmapping, etc on stored data such as arrays, collections, or I/O resources. That means, collections are mainly about data and streams are mainly about operations on data.

2. You can add to or remove elements from collections. But, you can’t add to or remove elements from streams. Stream consumes a source, performs operations on it, and returns a result. They don’t modify even the source also.

In Streams, there are no such methods to add or remove elements.

3. The main specialty of Java 8 Streams is that you need not worry about iteration while using Streams. Streams perform iteration internally behind the scene for you (using the forEach() method). You just have to mention the operations to be performed on a source.

On the other hand, you have to do the iteration externally over collections using loops.

4. Streams are traversable only once. If you traverse the stream once, it is said to be consumed. To traverse it again, you have to get a new stream from the source again. But, collections can be traversed multiple times.

Creating Empty Streams:

The empty() method should be used in case of the creation of an empty stream:

Stream<String> stream = Stream.empty();
stream.forEach(System.out::println);


Creating Stream from Collections:
A stream can be created of any type of Collection (Collection, List, Set):
Collection<String> collection = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
Stream<String> stream2 = collection.stream();
stream2.forEach(System.out::println);

List<String> list = Arrays.asList("JAVA", "J2EE", "Spring", "Hibernate");
Stream<String> stream3 = list.stream();
stream3.forEach(System.out::println);

Set<String> set = new HashSet<>(list);
Stream<String> stream4 = set.stream();
stream4.forEach(System.out::println);

Creating Stream object from Arrays:
An Array can be a source of a Stream or an Array can be created from the existing array or of a part of an array:
Stream<String> streamOfArray = Stream.of("a", "b", "c");
streamOfArray.forEach(System.out::println);
Creating Stream object from String using char() method:
We can also use String as a source for creating a stream with the help of the chars() method of the String class.
IntStream streamOfChars = "abc".chars();

Stream operations are divided into intermediate and terminal operations,

Intermediate operations transform a stream into another stream, such as filtering, sorting, element transformation (mapping)

Java Stream Example: Filtering using Stream.filter() method:
List < Product > productsList = new ArrayList < Product > ();

// Adding Products
productsList.add(new Product(1, "HP Laptop", 25000 f));
productsList.add(new Product(2, "Dell Laptop", 30000 f));
productsList.add(new Product(3, "Lenevo Laptop", 28000 f));
productsList.add(new Product(4, "Sony Laptop", 28000 f));
productsList.add(new Product(5, "Apple Laptop", 90000 f));

// Converting product List into Set
Set < Float > productPriceList = productsList.stream().filter(product -> product.getPrice() < 30000)
.map(product -> product.getPrice()).collect(Collectors.toSet());
System.out.println(productPriceList);terminal operation that produces a result, such as a forEach, min, max, count, sum, or a new collection.

List<String> stringList = new ArrayList<>();

stringList.add("one");
stringList.add("two");
stringList.add("three");
stringList.add("one");

Stream<String> stream = stringList.stream();

stream.forEach( element -> { System.out.println(element); });

output:
one 
two 
three 
one



Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

You can use Stream to filter, collect, print, and convert from one data structure to another, etc.
Stream does not store elements. It simply conveys elements from a source such as a data structure, an array, or an I/O channel, through a pipeline of computational operations.
Stream is functional in nature. Operations performed on a stream do not modify its source. For example, filtering a Stream obtained from a collection produces a new Stream without the filtered elements, rather than removing elements from the source collection.