Straumes karte (funkciju kartētājs) atgriež straumi, kas sastāv no dotās funkcijas piemērošanas rezultātiem šīs straumes elementiem.
Straumes karte (funkciju kartētājs) ir starpposma darbība . Šīs darbības vienmēr ir slinkas. Starpposma darbības tiek izsauktas Stream instancē, un pēc apstrādes pabeigšanas tās kā izvadi piešķir straumes gadījumu.
Sintakse :
< R>Straume < R>karte (Funkcija < ? super T , ? extends R>kartētājs), kur R ir jaunās straumes elementa veids. Straume ir saskarne, un T ir straumes elementu veids. kartētājs ir bezstāvokļa funkcija, kas tiek lietota katram elementam, un funkcija atgriež jauno straumi.>> 1. piemērs: Stream map() funkcija ar numuru * 3 katrā straumes elementā.
// 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 ->numurs *>> Izvade: The stream after applying the function is : 9 18 27 36 45 2. piemērs: Straumēšanas kartes() funkcija ar mazo burtu pārveidošanu par lielajiem burtiem. // 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);> > }> }> | Izvade: The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S] 3. piemērs: Straumes karte() funkcija ar virknes garuma kartēšanas darbību virknes vietā. // 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);>> Izvade: The stream after applying the function is : 5 3 9 8 7 3
| |