Sobes.tech
Junior

Map() metodunda hansı funksional interfeys ilə işləyirik?

sobes.tech Süni İntellekt

AI-dan cavab

Java axınlarının map() metodunda, biz java.util.function.Function funksional interfeys ilə işləyirik.

// `map()` metodunda Function istifadə nümunəsi
import java.util.List;
import java.util.stream.Collectors;

List<String> words = List.of("hello", "world");

// Function<String, Integer> String-i Integer-ə çevirir
List<Integer> lengths = words.stream()
                             .map(s -> s.length()) // Lambda ifadəsi Function<String, Integer>-i həyata keçirir
                             .collect(Collectors.toList());

// Digər üsul, method reference ilə
List<Integer> lengthsMethodRef = words.stream()
                                      .map(String::length) // Method reference də Function<String, Integer>-i həyata keçirir
                                      .collect(Collectors.toList());

Function<T, R> interfeysi T tipində arqument qəbul edir və R tipində nəticə qaytarır. map() kontekstində, T giriş axınının elementlərinin tipi, R isə çıxış axınının elementlərinin tipidir.