当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Stream flatMap()用法及代码示例


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]


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Stream flatMap() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。