#216 Generic Collection Transformation with Function
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:
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
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor