Srauto žemėlapis (funkcijų žemėlapis) grąžina srautą, susidedantį iš duotosios funkcijos taikymo šio srauto elementams rezultatų.
nepasirašytas int c programavimas
Srauto žemėlapis (funkcijų žemėlapis) yra tarpinė operacija . Šios operacijos visada yra tingios. Tarpinės operacijos iškviečiamos srauto egzemplioriuje, o baigus apdoroti, jos pateikia srauto egzempliorių kaip išvestį.
Sintaksė:
< R>Srautas< R>žemėlapis (Funkcija< ? super T , ? extends R>kartografas), kur R yra naujo srauto elemento tipas. Srautas yra sąsaja, o T yra srauto elementų tipas. Mapper yra funkcija be būsenos, kuri taikoma kiekvienam elementui ir funkcija grąžina naują srautą.>>1 pavyzdys: Srauto žemėlapio() funkcija su numeriu * 3 kiekvienam srauto elementui.
// 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 ->numeris *>> >}> }> |
>
Išvestis:
The stream after applying the function is : 9 18 27 36 45>
2 pavyzdys: Srautinio žemėlapio () funkcija su mažųjų raidžių konvertavimu į didžiąsias.
// 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);> >}> }> |
>
>
Išvestis:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
3 pavyzdys: Funkcija „Stream map()“ su atvaizdavimo eilutės ilgiu vietoj eilutės.
alisa manyonok
// 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);>> }> |
>
>
Išvestis:
The stream after applying the function is : 5 3 9 8 7 3>