Stream map() en Java con ejemplos

Mapa de flujo (mapeador de funciones) devuelve una secuencia que consta de los resultados de aplicar la función dada a los elementos de esta secuencia.

Stream map(Mapeador de funciones) es un operación intermedia . Estas operaciones siempre son vagas. Las operaciones intermedias se invocan en una instancia de Stream y, una vez que finalizan su procesamiento, dan una instancia de Stream como salida.

Sintaxis:

  < R>Arroyo < R>mapa(Función < ? super T , ? extends R>mapper) donde, R es el tipo de elemento de la nueva secuencia. Stream es una interfaz y T es el tipo de elementos de flujo. mapper es una función sin estado que se aplica a cada elemento y la función devuelve la nueva secuencia. 

Ejemplo 1 : Función Stream map() con operación del número * 3 en cada elemento de la secuencia.




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

Producción :

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

Ejemplo 2: Función Stream map() con operación de conversión de minúsculas a mayúsculas.




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

Producción :

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

Ejemplo 3: Función Stream map() con operación de mapeo de la longitud de la cadena en lugar de la cadena.




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

Producción :

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