Streamen Sie map() in Java mit Beispielen
Stream-Map (Funktions-Mapper) gibt einen Stream zurück, der aus den Ergebnissen der Anwendung der angegebenen Funktion auf die Elemente dieses Streams besteht.
Stream Map (Function Mapper) ist eine Zwischenbetrieb . Diese Operationen sind immer faul. Zwischenvorgänge werden für eine Stream-Instanz aufgerufen und geben nach Abschluss ihrer Verarbeitung eine Stream-Instanz als Ausgabe aus.
Syntax :
< R>Strom < R>Karte(Funktion < ? super T , ? extends R>Mapper), wobei R der Elementtyp des neuen Streams ist. Stream ist eine Schnittstelle und T ist der Typ der Stream-Elemente. Mapper ist eine zustandslose Funktion, die auf jedes Element angewendet wird und den neuen Stream zurückgibt.
Beispiel 1 : Stream-Map()-Funktion mit der Operation Nummer * 3 für jedes Element des Streams.
// 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 ->Nummer *> 3> ).forEach(System.out::println);> > }> }> |
Ausgabe :
The stream after applying the function is : 9 18 27 36 45
Beispiel 2: Stream-Map()-Funktion mit der Konvertierung von Kleinbuchstaben in Großbuchstaben.
// 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);> > }> }> |
Ausgabe :
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]
Beispiel 3: Streamen Sie die Funktion „map()“ mit der Operation „Zuordnen der Zeichenfolgenlänge anstelle der Zeichenfolge“.
// 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);> > }> }> |
Ausgabe :
The stream after applying the function is : 5 3 9 8 7 3