Streamkaart (Functietoewijzer) retourneert een stroom die bestaat uit de resultaten van het toepassen van de gegeven functie op de elementen van deze stroom.
wachtrij en prioriteitswachtrij in Java
Stream map (Functie mapper) is een tussenliggende werking . Deze operaties zijn altijd lui. Tussenliggende bewerkingen worden aangeroepen op een Stream-instantie en nadat ze hun verwerking hebben voltooid, geven ze een Stream-instantie als uitvoer.
Syntaxis:
< R>Stroom< R>kaart(Functie< ? super T , ? extends R>mapper) waarbij R het elementtype van de nieuwe stream is. Stream is een interface en T is het type streamelementen. mapper is een staatloze functie die op elk element wordt toegepast en de functie retourneert de nieuwe stream.>
Voorbeeld 1 : Stream map()-functie met bewerking van nummer * 3 op elk element van de 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 ->nummer *>3>).forEach(System.out::println);> >}> }> |
>
>
Uitgang:
The stream after applying the function is : 9 18 27 36 45>
Voorbeeld 2: Stream map()-functie met bewerking van het converteren van kleine letters naar hoofdletters.
// 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);> >}> }> |
>
>
Uitgang:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Voorbeeld 3: Stream map()-functie met bewerking van het in kaart brengen van de tekenreekslengte in plaats van de tekenreeks.
tekenreeks is leeg
// 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);> >}> }> |
>
>
Uitgang:
The stream after applying the function is : 5 3 9 8 7 3>