logo

Stream map() v Javi s primeri

Preslikava toka (preslikava funkcij) vrne tok, sestavljen iz rezultatov uporabe dane funkcije za elemente tega toka.

kateri meseci so q1

Preslikava toka (preslikava funkcij) je vmesno delovanje . Te operacije so vedno lene. Vmesne operacije se prikličejo na instanci Stream in potem, ko končajo obdelavo, dajo kot izhod primerek Stream.



Sintaksa:

 < R>Tok< R>zemljevid (funkcija< ? super T , ? extends R>preslikava), kjer je R tip elementa novega toka. Tok je vmesnik in T je vrsta elementov toka. Mapper je funkcija brez stanja, ki se uporabi za vsak element in funkcija vrne nov tok.>

Primer 1: Funkcija mapa toka() z operacijo števila * 3 na vsakem elementu toka.








// 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 ->številka *>3>).forEach(System.out::println);> >}> }>

>

>

Izhod:

 The stream after applying the function is : 9 18 27 36 45>

Primer 2: Funkcija Stream map() s pretvorbo malih črk v velike.




// 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);> >}> }>

>

>

Izhod:

 The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>

Primer 3: Funkcija preslikave toka() z operacijo preslikave dolžine niza namesto niza.




sončni del

// 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);> >}> }>

>

>

Izhod:

 The stream after applying the function is : 5 3 9 8 7 3>