Stream flatMap(Function mapper)返回一個流,該流包括將流中的每個元素替換為通過將提供的映射函數應用於每個元素而生成的映射流的內容而得到的結果。流flatMap(Function mapper)是一個中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
注意:將每個映射流的內容放入該流後,將其關閉。如果映射的流為null,則使用空流。
flatMap() V /秒map():
1)map()接收一個Stream並將其轉換為另一個Stream。它在Stream的每個元素上應用一個函數,並將返回值存儲到新的Stream中。它不會使流變平。但是flatMap()是映射和平麵操作的組合,即,它對元素應用了一個函數並對其進行了展平。
2)map()僅用於變換,但flatMap()用於變換和展平。
用法:
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) where, R is the element type of the new stream. Stream is an interface and T is the type of stream elements. mapper is a stateless function which is applied to each element and the function returns the new stream.
示例1:具有提供的映射函數的flatMap()函數。
// Java code for Stream flatMap
// (Function mapper) to get a stream by
// replacing the stream with a mapped
// stream by applying the provided mapping function.
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a List of Strings
List<String> list = Arrays.asList("5.6", "7.4", "4",
"1", "2.3");
// Using Stream flatMap(Function mapper)
list.stream().flatMap(num -> Stream.of(num)).
forEach(System.out::println);
}
}
輸出:
5.6 7.4 4 1 2.3
示例2:flatMap()函數提供了在位置2處具有字符的映射字符串的操作。
// Java code for Stream flatMap
// (Function mapper) to get a stream by
// replacing the stream with a mapped
// stream by applying the provided mapping function.
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a List of Strings
List<String> list = Arrays.asList("Geeks", "GFG",
"GeeksforGeeks", "gfg");
// Using Stream flatMap(Function mapper)
list.stream().flatMap(str ->
Stream.of(str.charAt(2))).
forEach(System.out::println);
}
}
輸出:
e G e g
How does flatMap() work ?
正如文章中已經討論過的那樣,flatMap()是映射和展開操作的組合,即,它首先應用了映射函數,然後將結果展平。讓我們考慮一些示例,以了解什麽是完全平展流。
示例1:
展平前的列表:
[ [2, 3, 5], [7, 11, 13], [17, 19, 23] ]
該列表分為2級,由3個小列表組成。展平後,將其轉換為“one level”結構,如下所示:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
示例2:
展平前的列表:
[ ["G", "E", "E"], ["K", "S", "F"], ["O", "R", "G"], ["E", "E", "K", "S"] ]
該列表分為3級,由4個小列表組成。展平後,將其轉換為“one level”結構,如下所示:
["G", "E", "E", "K", "S", "F", "O", "R", "G", "E", "E", "K", "S"]
簡而言之,我們可以說,如果在展平之前存在數據類型>>的流列表,則在應用flatMap()時,在展平之後將返回數據類型>>的流。
應用:
// Java code for Stream flatMap(Function mapper)
import java.util.*;
import java.util.stream.Collectors;
class GFG
{
// Driver code
public static void main(String[] args)
{
// Creating a list of Prime Numbers
List<Integer> PrimeNumbers = Arrays.asList(5, 7, 11,13);
// Creating a list of Odd Numbers
List<Integer> OddNumbers = Arrays.asList(1, 3, 5);
// Creating a list of Even Numbers
List<Integer> EvenNumbers = Arrays.asList(2, 4, 6, 8);
List<List<Integer>> listOfListofInts =
Arrays.asList(PrimeNumbers, OddNumbers, EvenNumbers);
System.out.println("The Structure before flattening is : " +
listOfListofInts);
// Using flatMap for transformating and flattening.
List<Integer> listofInts = listOfListofInts.stream()
.flatMap(list -> list.stream())
.collect(Collectors.toList());
System.out.println("The Structure after flattening is : " +
listofInts);
}
}
輸出:
The Structure before flattening is : [[5, 7, 11, 13], [1, 3, 5], [2, 4, 6, 8]] The Structure after flattening is : [5, 7, 11, 13, 1, 3, 5, 2, 4, 6, 8]
相關用法
- Java Stream map()用法及代碼示例
- Java Stream findAny()用法及代碼示例
- Java Stream findFirst()用法及代碼示例
- Java Stream.max()用法及代碼示例
- Java Stream anyMatch()用法及代碼示例
- Java Stream filter()用法及代碼示例
- Java Stream.reduce()用法及代碼示例
- Java Stream flatMapToDouble()用法及代碼示例
- Java Stream flatMapToLong()用法及代碼示例
- Java Stream flatMapToInt()用法及代碼示例
- Java Stream peek()用法及代碼示例
- Java Stream mapToInt()用法及代碼示例
- Java Stream builder()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Stream flatMap() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。