Harta fluxului (Mapper funcții) returnează un flux format din rezultatele aplicării funcției date la elementele acestui flux.
preity zinta
Harta fluxului (Function mapper) este un operație intermediară . Aceste operațiuni sunt întotdeauna leneșe. Operațiunile intermediare sunt invocate pe o instanță Stream și, după ce își termină procesarea, dau o instanță Stream ca ieșire.
Sintaxa:
< R>Curent< R>harta (Funcția< ? super T , ? extends R>mapper) unde, R este tipul de element al noului flux. Fluxul este o interfață și T este tipul de elemente de flux. mapper este o funcție fără stat care este aplicată fiecărui element, iar funcția returnează noul flux.>>>Exemplul 1: Funcția Stream map() cu funcționarea numărului * 3 pe fiecare element al fluxului.
// 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ărul *>>> >}> }> |
>
Ieșire:
The stream after applying the function is : 9 18 27 36 45>
Exemplul 2: Funcția Stream map() cu operațiune de conversie a literelor mici în majuscule.
// 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);> >}> }> |
>
>
Ieșire:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Exemplul 3: Funcția Stream map() cu operarea de mapare a lungimii șirului în loc de șir.
variabile de tip java
// 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);>>> }> |
>
>
Ieșire:
The stream after applying the function is : 5 3 9 8 7 3>