Mapa streamu (mapovač funkcí) vrací proud sestávající z výsledků aplikace dané funkce na prvky tohoto proudu.
středový obrázek v css
Streamová mapa (mapovač funkcí) je střední provoz . Tyto operace jsou vždy líné. Mezilehlé operace jsou vyvolány na instanci Stream a poté, co dokončí své zpracování, poskytnou instanci Stream jako výstup.
Syntaxe:
< R>Proud< R>mapa (Funkce< ? super T , ? extends R>mapper), kde R je typ prvku nového proudu. Stream je rozhraní a T je typ prvků toku. mapper je bezstavová funkce, která je aplikována na každý prvek a funkce vrací nový proud.>
Příklad 1: Funkce stream map() s operací čísla * 3 na každém prvku streamu.
// 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 ->číslo *>3>).forEach(System.out::println);> >}> }> |
>
>
Výstup :
The stream after applying the function is : 9 18 27 36 45>
Příklad 2: Funkce stream map() s operací převodu malých písmen na velká.
js settimeout
// 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);> >}> }> |
>
>
Výstup :
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Příklad 3: Funkce stream map() s operací mapování délky řetězce místo řetězce.
// 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);> >}> }> |
>
>
Výstup :
The stream after applying the function is : 5 3 9 8 7 3>