Java Interview Preparation - Streams
Published on

Java Interview Preparation - Streams

Authors

Different functions in Streams

  • map() - The map() function is used to transform one object into other by applying a function.
  • filter() - The filter() function is used to filter the elements from a stream.
  • collect() - The collect() function is used to collect the elements from a stream.
  • reduce() - The reduce() function is used to reduce the elements of a stream to a single value.
  • forEach() - The forEach() function is used to iterate over the elements of a stream.
  • flatMap() - The flatMap() function is used to flatten the elements of a stream.
  • distinct() - The distinct() function is used to remove the duplicate elements from a stream.
  • sorted() - The sorted() function is used to sort the elements of a stream.
  • limit() - The limit() function is used to limit the number of elements in a stream.
  • skip() - The skip() function is used to skip the elements of a stream.
  • anyMatch() - The anyMatch() function is used to check if any element of a stream matches the given predicate.
  • allMatch() - The allMatch() function is used to check if all elements of a stream match the given predicate.
  • noneMatch() - The noneMatch() function is used to check if none of the elements of a stream match the given predicate.
  • findAny() - The findAny() function is used to find any element of a stream.
  • findFirst() - The findFirst() function is used to find the first element of a stream.
  • count() - The count() function is used to count the number of elements in a stream.
  • min() - The min() function is used to find the minimum element of a stream.
  • max() - The max() function is used to find the maximum element of a stream.

What is the difference between map() and flatMap() functions in Streams?

  1. map(Function mapper): This function applies a given function to each element of the stream and incorporates its result into the new stream. The function is applied to each element, transforming it, but does not flatten or combine the elements. If the function returns a complex structure like a List or Stream, the result will be a Stream of those structures.
List<String> words = Arrays.asList("Hello", "World");
Stream<List<Integer>> lengths = words.stream().map(word -> Arrays.asList(word.length()));
// Result: Stream containing two lists, each with one integer
  1. flatMap(Function mapper): This function is similar to map, but it has an additional step. After applying the function to each element, it flattens the result into a new stream. So, if the function returns a complex structure like a List or Stream, flatMap will flatten that into individual elements.
List<String> words = Arrays.asList("Hello", "World");
Stream<Integer> lengths = words.stream().flatMap(word -> word.chars().boxed());

Which steam functions are intermediate and terminal?

  • Intermediate functions: map(), filter(), flatMap(), distinct(), sorted(), limit(), skip(), peek(), etc.
  • Terminal functions: collect(), reduce(), forEach(), anyMatch(), allMatch(), noneMatch(), findAny(), findFirst(), count(), min(), max(), etc.