Stream kort (funktionsmapper) returnerer en strøm, der består af resultaterne af at anvende den givne funktion på elementerne i denne strøm.
de er sangere
Stream map(Function Mapper) er en mellemdrift . Disse operationer er altid dovne. Mellemliggende operationer påkaldes på en Stream-instans, og efter at de er færdige med deres behandling, giver de en Stream-instans som output.
Syntaks:
< R>Strøm< R>kort (funktion< ? super T , ? extends R>mapper), hvor R er elementtypen for den nye strøm. Stream er en grænseflade, og T er typen af streamelementer. mapper er en tilstandsløs funktion, som anvendes på hvert element, og funktionen returnerer den nye strøm.>
Eksempel 1: Stream map() funktion med drift af nummer * 3 på hvert element i 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);> >}> }> |
>
>
Output:
The stream after applying the function is : 9 18 27 36 45>
Eksempel 2: Stream map() funktion med operation for at konvertere små bogstaver til store bogstaver.
java streng til json
// 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);> >}> }> |
>
>
Output:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
Eksempel 3: Stream map() funktion med betjening af kortlægning af strenglængde i stedet for streng.
// 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);> >}> }> |
>
>
Output:
The stream after applying the function is : 5 3 9 8 7 3>