Stream map() w Javie z przykładami

Mapa strumienia (mapa funkcji) zwraca strumień składający się z wyników zastosowania danej funkcji do elementów tego strumienia.

Mapa strumienia (mapa funkcji) to plik operacja pośrednia . Te operacje są zawsze leniwe. Operacje pośrednie są wywoływane na instancji Stream i po zakończeniu przetwarzania dają instancję Stream jako wynik.

Składnia:

  < R>Strumień < R>mapa(funkcja < ? super T , ? extends R>mapper), gdzie R jest typem elementu nowego strumienia. Stream to interfejs, a T to typ elementów strumienia. mapper to funkcja bezstanowa, która jest stosowana do każdego elementu i zwraca nowy strumień. 

Przykład 1 : Funkcja Stream map() z operacją liczby * 3 na każdym elemencie strumienia.




// Java code for Stream map(Function mapper)> // to get a stream by applying the> // given function to this stream.> import> java.util.*;> > class> GFG {> > > // Driver code> > public> static> void> main(String[] args)> > {> > > System.out.println(> 'The stream after applying '> > +> 'the function is : '> );> > > // Creating a list of Integers> > List list = Arrays.asList(> 3> ,> 6> ,> 9> ,> 12> ,> 15> );> > > // Using Stream map(Function mapper) and> > // displaying the corresponding new stream> > list.stream().map(number ->numer *> 3> ).forEach(System.out::println);> > }> }>

Wyjście :

 The stream after applying the function is : 9 18 27 36 45 

Przykład 2: Funkcja Stream map() z operacją konwersji małych liter na wielkie.




// Java code for Stream map(Function mapper)> // to get a stream by applying the> // given function to this stream.> import> java.util.*;> import> java.util.stream.Collectors;> > class> GFG {> > > // Driver code> > public> static> void> main(String[] args)> > {> > > System.out.println(> 'The stream after applying '> > +> 'the function is : '> );> > > // Creating a list of Integers> > List list = Arrays.asList(> 'geeks'> ,> 'gfg'> ,> 'g'> ,> > 'e'> ,> 'e'> ,> 'k'> ,> 's'> );> > > // Using Stream map(Function mapper) to> > // convert the Strings in stream to> > // UpperCase form> > List answer = list.stream().map(String::toUpperCase).> > collect(Collectors.toList());> > > // displaying the new stream of UpperCase Strings> > System.out.println(answer);> > }> }>

Wyjście :

 The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S] 

Przykład 3: Funkcja Stream map() z operacją mapowania długości stringu w miejsce stringa.




// Java code for Stream map(Function mapper)> // to get a stream by applying the> // given function to this stream.> import> java.util.*;> > class> GFG {> > > // Driver code> > public> static> void> main(String[] args)> > {> > > System.out.println(> 'The stream after applying '> > +> 'the function is : '> );> > > // Creating a list of Strings> > List list = Arrays.asList(> 'Geeks'> ,> 'FOR'> ,> 'GEEKSQUIZ'> ,> > 'Computer'> ,> 'Science'> ,> 'gfg'> );> > > // Using Stream map(Function mapper) and> > // displaying the length of each String> > list.stream().map(str ->str.length()).forEach(System.out::println);> > }> }>

Wyjście :

 The stream after applying the function is : 5 3 9 8 7 3