Stream map() in Java con esempi
Mappa del flusso (mappatore di funzioni) restituisce un flusso costituito dai risultati dell'applicazione della funzione data agli elementi di questo flusso.
La mappa del flusso (mappatore di funzioni) è un file operazione intermedia . Queste operazioni sono sempre pigre. Le operazioni intermedie vengono richiamate su un'istanza di Stream e una volta terminata l'elaborazione, forniscono un'istanza di Stream come output.
Sintassi:
< R>Flusso < R>mappa(Funzione < ? super T , ? extends R>mapper) dove R è il tipo di elemento del nuovo flusso. Stream è un'interfaccia e T è il tipo di elementi dello stream. mapper è una funzione senza stato che viene applicata a ciascun elemento e la funzione restituisce il nuovo flusso.
Esempio 1 : Funzione Stream map() con operazione di numero * 3 su ciascun elemento dello stream.
// 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);> > }> }> |
Produzione :
The stream after applying the function is : 9 18 27 36 45
Esempio 2: Funzione Stream map() con operazione di conversione da minuscolo a maiuscolo.
// 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);> > }> }> |
Produzione :
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]
Esempio 3: Funzione Stream map() con operazione di mappatura della lunghezza della stringa al posto della stringa.
// 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);> > }> }> |
Produzione :
The stream after applying the function is : 5 3 9 8 7 3