Mapa de flux (Mapeador de funcions) retorna un flux que consisteix en els resultats d'aplicar la funció donada als elements d'aquest flux.
on trobo la configuració del meu navegador
El mapa de flux (Mapeador de funcions) és un operació intermèdia . Aquestes operacions sempre són mandroses. Les operacions intermèdies s'invoquen en una instància de Stream i després d'acabar el seu processament, donen una instància de Stream com a sortida.
Sintaxi:
< R>corrent< R>mapa (Funció< ? super T , ? extends R>mapper) on, R és el tipus d'element del nou flux. El flux és una interfície i T és el tipus d'elements del flux. mapper és una funció sense estat que s'aplica a cada element i la funció retorna el nou flux.>>>Exemple 1: Funció Stream map() amb l'operació del número * 3 a cada element del flux.
// 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 ->número *>>> >}> }> |
>
Sortida:
The stream after applying the function is : 9 18 27 36 45>
Exemple 2: Funció Stream map() amb operació de conversió de minúscules a majúscules.
// 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);> >}> }> |
longitud de la cadena java
>
>
Sortida:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Exemple 3: Funció Stream map() amb l'operació de mapejar la longitud de la cadena en lloc de la cadena.
// 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);>>> }> |
>
>
Sortida:
The stream after applying the function is : 5 3 9 8 7 3>