Stream流中的Map与flatMap的区别
Map与flatMap的基本概念
Map用于将流中的每个元素通过指定函数转换为另一个元素,生成一个一对一映射的新流。例如将字符串流转换为对应长度流:
List<String> words = Arrays.asList("a", "bb", "ccc");
List<Integer> lengths = words.stream().map(String::length).collect(Collectors.toList());
// 输出: [1, 2, 3]
flatMap用于将每个元素转换为流后合并所有流,形成一对多映射的扁平化结果。例如拆分字符串中的单词:
List<String> lines = Arrays.asList("hello world", "java stream");
List<String> words = lines.stream().flatMap(line -> Arrays.stream(line.split(" "))).collect(Collectors.toList());
// 输出: ["hello", "world", "java", "stream"]
核心区别对比
-
输入输出关系
Map保持原始流元素数量不变,flatMap可能改变元素数量(如拆分为多个子流后合并) -
返回值要求
Map的Function返回普通对象,flatMap的Function必须返回Stream对象 -
数据结构处理
Map适合单一元素转换,flatMap适合处理嵌套集合(如List<List>)
典型应用场景
Map适用场景
数据类型转换(String→Integer)、属性提取(对象→ID)、简单计算(数值→平方值)
flatMap适用场景
- 合并多个集合:
List<List<Integer>> → List<Integer>
- 分解复合结构:字符串句子→单词流、树节点→子树节点流
- 过滤后展开操作:先filter再flatMap处理有效数据
性能注意事项
flatMap涉及流合并操作,可能产生更多临时对象。对于简单转换优先使用map,多层嵌套数据结构才使用flatMap。
代码示例对比
处理书籍作者场景:
// 使用map(保留嵌套结构)
List<Book> books = ...;
List<List<String>> authors = books.stream().map(Book::getAuthors).collect(Collectors.toList());// 使用flatMap(扁平化结构)
List<String> allAuthors = books.stream().flatMap(book -> book.getAuthors().stream()).collect(Collectors.toList());