Suoratoista karttaa (toimintokartoittaja) palauttaa virran, joka koostuu tuloksista, jotka on saatu annetun funktion soveltamisesta tämän virran elementteihin.
tiedon louhinta
Suoratoistokartta (Function Mapper) on välitoiminta . Nämä toiminnot ovat aina laiskoja. Välioperaatioita kutsutaan Stream-instanssissa, ja niiden käsittelyn päätyttyä ne antavat Stream-ilmentymän tulosteena.
Syntaksi :
< R>Striimaa< R>kartta (toiminto< ? super T , ? extends R>mapper), jossa R on uuden virran elementtityyppi. Stream on käyttöliittymä ja T on stream-elementtien tyyppi. Mapper on tilaton funktio, jota sovelletaan jokaiseen elementtiin ja funktio palauttaa uuden virran.>>Esimerkki 1: Stream map()-funktio, jonka toiminto on numero * 3 jokaisessa stream-elementissä.
// 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 ->numero *>3>).forEach(System.out::println);> >}> }> |
>
Lähtö:
The stream after applying the function is : 9 18 27 36 45>
Esimerkki 2: Stream map()-toiminto, joka muuntaa pienet kirjaimet isoiksi.
java päivämäärä merkkijonoon
// 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);> >}> }> |
>
>
Lähtö:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Esimerkki 3: Stream map()-funktio, jossa merkkijonon pituuden kartoitus merkkijonon sijaan.
// 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);>> }> |
>
>
Lähtö:
The stream after applying the function is : 5 3 9 8 7 3>