Carte de flux (mappeur de fonctions) renvoie un flux composé des résultats de l'application de la fonction donnée aux éléments de ce flux.
0,04 en fraction
La carte de flux (mappeur de fonctions) est un opération intermédiaire . Ces opérations sont toujours paresseuses. Les opérations intermédiaires sont invoquées sur une instance Stream et une fois leur traitement terminé, elles donnent une instance Stream en sortie.
Syntaxe :
< R>Flux< R>carte(Fonction< ? super T , ? extends R>mapper) où R est le type d’élément du nouveau flux. Stream est une interface et T est le type d'éléments de flux. mapper est une fonction sans état qui est appliquée à chaque élément et la fonction renvoie le nouveau flux.>
Exemple 1 : Fonction Stream map() avec opération du nombre * 3 sur chaque élément du stream.
// 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 ->numéro *>3>).forEach(System.out::println);> >}> }> |
>
>
Sortir :
The stream after applying the function is : 9 18 27 36 45>
Exemple 2 : Fonction Stream map() avec opération de conversion des minuscules en majuscules.
// 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);> >}> }> |
>
>
Sortir :
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Exemple 3 : Fonction Stream map() avec opération de mappage de la longueur de la chaîne à la place de la chaîne.
1 million combien 0
// 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);> >}> }> |
>
>
Sortir :
The stream after applying the function is : 5 3 9 8 7 3>