Sobes.tech
Junior — Middle

What type of functional interface is used when applying the map method in Stream API?

sobes.tech AI

Answer from AI

The map method in Java's Stream API takes a functional interface Function<T, R> as a parameter, where:

  • T is the type of the input element of the stream
  • R is the type of the output element after transformation

This interface defines the method R apply(T t), which transforms an element of type T into an element of type R.

Example:

List<String> strings = Arrays.asList("1", "2", "3");
List<Integer> numbers = strings.stream()
    .map(Integer::parseInt) // Function<String, Integer>
    .collect(Collectors.toList());

Here, map takes a Function<String, Integer>, which converts strings to numbers.

What type of functional interface is used when… - sobes.tech