#216 Generic Collection Transformation with Function

Companies:
Alfa-Bank
Themes:
JavaCollections & GenericsStreams & LambdasPractice

Implement a generic method that transforms a collection of objects of one type into a collection of another type, for example transforming a collection of String values into a collection of Integer values. The method must accept a collection and a function that converts one type into another:

Java
public class Main {

    public static void main(String[] args) {
        List<String> list1 = List.of("1", "2", "3");
        List<Integer> integers = change(list1, Integer::parseInt);
    }

    public static <T, R> List<R> change(List<T> collection, Function<T, R> func) {
        ...
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in