ストリームマップ(関数マッパー) 指定された関数をこのストリームの要素に適用した結果で構成されるストリームを返します。
私のiPhoneアンドロイドを見つけてください
ストリームマップ(関数マッパー)は 中間操作 。これらの操作は常に遅延的です。中間操作は Stream インスタンスで呼び出され、処理が完了すると、Stream インスタンスが出力として提供されます。
構文:
< R>ストリーム< R>マップ(関数< ? super T , ? extends R>マッパー) ここで、R は新しいストリームの要素タイプです。 Stream はインターフェイス、T はストリーム要素のタイプです。マッパーは各要素に適用されるステートレス関数であり、関数は新しいストリームを返します。>>
例 1: ストリームの各要素に対して数値 * 3 の演算を行うストリーム マップ() 関数。
// 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 ->番号 *>>3>).forEach(System.out::println);> >}> }> |
>
>
出力:
The stream after applying the function is : 9 18 27 36 45>
例 2: 小文字を大文字に変換する操作を含む Stream map() 関数。
// 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);> >}> }> |
>
一次ロジック
>
出力:
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]>
例 3: 文字列の代わりに文字列の長さをマッピングする操作を伴う Stream map() 関数。
// 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);>> >}> }> |
>
>
出力:
The stream after applying the function is : 5 3 9 8 7 3>