Junior
Sa kojim funkcionalnim interfejsom radimo u metodi map()?
sobes.tech АИ
Одговор од АИ
U metodu map() Java tokovima, radimo sa funkcionalnim interfejsom java.util.function.Function.
// Primer korišćenja Function u metodu map()
import java.util.List;
import java.util.stream.Collectors;
List<String> words = List.of("hello", "world");
// Function<String, Integer> pretvara String u Integer
List<Integer> lengths = words.stream()
.map(s -> s.length()) // Lambda izraz implementira Function<String, Integer>
.collect(Collectors.toList());
// Drugi način sa referencom na metodu
List<Integer> lengthsMethodRef = words.stream()
.map(String::length) // Referenca na metodu takođe implementira Function<String, Integer>
.collect(Collectors.toList());
Interfejs Function<T, R> prihvata jedan argument tipa T i vraća rezultat tipa R. U kontekstu map(), T je tip elemenata ulaznog toka, a R je tip elemenata izlaznog toka.